❮ Python Glossary
String Concatenation
String concatenation means add strings together.
Use the + character to add a variable to another variable:
Example
x = "Python is "
y = "awesome"
z = x + y
print(z)
Try it Yourself »
Example
Merge variable a with variable b into variable c:
a = "Hello"
b = "World"
c = a + b
print(c)
Try it Yourself »
Example
To add a space between them, add a " ":
a = "Hello"
b = "World"
c = a + " " + b
print(c)
Try it Yourself »
For numbers, the + character works as a mathematical operator:
Example
x = 5
y = 10
print(x + y)
Try it Yourself »
If you try to combine a string and a number, Python will give you an error:
Example
x = 5
y = "John"
print(x + y)
Try it Yourself »
Related Pages
Python Variables Tutorial Creating Variables Variable Names Assign Value to Multiple Variables Output Variables Global Variables
❮ Python Glossary
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
var1 = "Welcome"
var2 = "statistics"
print(f"{var1} {var2}")
Output:
Welcome statisticsString 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
var1 = "Hello "
var2 = "World"
var3 = var1 + var2
print(var3)
Output
Hello WorldMethod 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"
print("".join([var1, var2]))
var3 = " ".join([var1, var2])
print(var3)
Output
HelloWorld Hello WorldMethod 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"
print("% s % s" % (var1, var2))
Output
Hello WorldMethod 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"
print("{} {}".format(var1, var2))
var3 = "{} {}".format(var1, var2)
print(var3)
Output
Hello World Hello WorldMethod 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"
print(var1, var2)
Output
Hello World