Python String Concatenation
In Python, Strings are arrays of bytes representing Unicode characters. However, Python does not have a character data type, a single character is simply a string with a length of 1. Square brackets [] can be used to access elements of the string.
Example:
Python3
# Assign variable var1 & var 2 var1 = "Welcome" var2 = "statistics" # print the result print (f "{var1} {var2}" ) |
Output:
Welcome statistics
String Concatenation in Python
String Concatenation is the technique of combining two strings. String Concatenation can be done using many ways.
- Using + operator
- Using join() method
- Using % operator
- Using format() function
- Using , (comma)
Method 1: String Concatenation using + Operator
It’s very easy to use the + operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string. Here, The + Operator combines the string that is stored in the var1 and var2 and stores in another variable var3.
Note: Strings are immutable, therefore, whenever it is concatenated, it is assigned to a new variable.
Python3
# Defining strings var1 = "Hello " var2 = "World" # + Operator is used to combine strings var3 = var1 + var2 print (var3) |
Output
Hello World
Method 2: String Concatenation using join() Method
The join() method is a string method and returns a string in which the elements of the sequence have been joined by str separator. This method combines the string that is stored in the var1 and var2. It accepts only the list as its argument and list size can be anything.
Python3
var1 = "Hello" var2 = "World" # join() method is used to combine the strings print ("".join([var1, var2])) # join() method is used here to combine # the string with a separator Space(" ") var3 = " " .join([var1, var2]) print (var3) |
Output
HelloWorld Hello World
The time complexity of both the methods is O(n), where n is the total length of the strings.
The auxiliary space complexity for the second join() method is also O(n), as it creates a new string that contains the combined string along with the separator.
Method 3: String Concatenation using % Operator
We can use the % operator for string formatting, it can also be used for string concatenation. It’s useful when we want to concatenate strings and perform simple formatting. The %s denotes string data type. The value in both the variable is passed to the string %s and becomes “Hello World”.
Python3
var1 = "Hello" var2 = "World" # % Operator is used here to combine the string print ( "% s % s" % (var1, var2)) |
Output
Hello World
Method 4: String Concatenation using format() function
str.format() is one of the string formatting methods in Python, which allows multiple substitutions and value formatting. It concatenate elements within a string through positional formatting. The curly braces {} are used to set the position of strings. The first variable stores in the first curly braces and the second variable stores in the second curly braces. Finally, it prints the value “Hello World”.
Python3
var1 = "Hello" var2 = "World" # format function is used here to # combine the string print ( "{} {}" . format (var1, var2)) # store the result in another variable var3 = "{} {}" . format (var1, var2) print (var3) |
Output
Hello World Hello World
Method 5: String Concatenation using (, comma)
“,” is a great alternative to string concatenation using “+”. when you want to include single whitespace. Use a comma when you want to combine data types with single whitespace in between.
Python3
var1 = "Hello" var2 = "World" # , to combine data types with a single whitespace. print (var1, var2) |
Output
Hello World
Please Login to comment...