Python String Concatenation

24 Aug, 2021
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
# Python program to demonstrate# strings  # Assign Welcome string to the variable var1var1 = "Welcome" # Assign statistics string to the variable var2var2 = "statistics" # print the resultprint(var1)print(var2)
Welcome
statistics
 
String Concatenation is the technique of combining two strings. String Concatenation can be done using many ways.We can perform string concatenation using following ways: 
Using + operator  Using join() method  Using % operator  Using format() function  Using , (comma) 
Using + operator  
Using join() method  
Using % operator  
Using format() function  
Using , (comma) 
 
It’s very easy to use + operator for string concatenation. This operator can be used to add multiple strings together. However, the arguments must be a string. Note: Strings are immutable, therefore, whenever it is concatenated, it is assigned to a new variable.Example: 
Python3
# Python program to demonstrate# string concatenation  # Defining stringsvar1 = "Hello "var2 = "World" # + Operator is used to combine stringsvar3 = var1 + var2print(var3)
Hello World
Here, the variable var1 stores the string “Hello ” and variable var2 stores the string “World”. The + Operator combines the string that is stored in the var1 and var2 and stores in another variable var3. 
The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator.Example: 
Python3
# Python program to demonstrate# string concatenation  var1 = "Hello"var2 = "World" # join() method is used to combine the stringsprint("".join([var1, var2])) # join() method is used here to combine# the string with a separator Space(" ")var3 = " ".join([var1, var2]) print(var3)
HelloWorld
Hello World
In the above example, the variable var1 stores the string “Hello” and variable var2 stores the string “World”. The join() method combines the string that is stored in the var1 and var2. The join method accepts only the list as it’s argument and list size can be anything. We can store the combined string in another variable var3 which is separated by space.Note: To know more about join() method click here. 
We can use % 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.Example: 
Python3
# Python program to demonstrate# string concatenation  var1 = "Hello"var2 = "World" # % Operator is used here to combine the stringprint("% s % s" % (var1, var2))
Hello World
Here, the % Operator combine the string that is stored in the var1 and var2. The %s denotes string data type. The value in both the variable is passed to the string %s and becomes “Hello World”. 
str.format() is one of the string formatting methods in Python, which allows multiple substitutions and value formatting. This method lets us concatenate elements within a string through positional formatting.Example: 
Python3
# Python program to demonstrate# string concatenation  var1 = "Hello"var2 = "World" # format function is used here to# combine the stringprint("{} {}".format(var1, var2)) # store the result in another variablevar3 = "{} {}".format(var1, var2) print(var3)
Hello World
Hello World
Here, the format() function combines the string that is stored in the var1 and var2 and stores in another variable var3. The curly braces {} are used to set the position of strings. The first variable stores in the first curly braces and second variable stores in the second curly braces. Finally it prints the value “Hello World”.Note: To know more about format() function click here.
“,” is a great alternative to string concatenation using “+”. when you want to include a single whitespace.Example: 
Python3
# Python program to demonstrate# string concatenation var1 = "Hello"var2 = "World" # , to combine data types with a single whitespace.print(var1, var2)
Hello World
Use , when you want to combine data types with a single whitespace in between.
srijanarya04
python-string
Python
Technical Scripter
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.