❮ 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
Summary: in this tutorial, you’ll learn various ways to concatenate strings in Python.
Python provides you with various ways to concatenate one or more strings into a new string.
Since Python string is immutable, the concatenation always results in a new string.
1] Concatenating literal strings
To concatenate two or more literal strings, you just need to place them next to each other. For example:
Code language: PHP [php]
s = 'String' ' Concatenation' print[s]
Output:
Code language: JavaScript [javascript]
String Concatenation
Note that this way won’t work for the string variables.
2] Concatenating strings using the + operator
A straightforward way to concatenate multiple strings into one is to use the + operator:
s = 'String' + ' Concatenation' print[s]
Code language: PHP [php]
And the + operator works for both literal strings and string variables. For example:
Code language: PHP [php]
s1 = 'String' s2 = s1 + ' Concatenation' print[s2]
Output:
Code language: JavaScript [javascript]
String Concatenation
3] Concatenating strings using the += operator
Similar to the + operator, you can use the += operator to concatenate multiple strings into one:
Code language: PHP [php]
s = 'String' s += ' Concatenation' print[s]
Output:
Code language: JavaScript [javascript]
String Concatenation
4] Concatenating strings using the join[] method
The join[]
method allows you to concatenate a list of strings into a single string:
Code language: PHP [php]
s1 = 'String' s2 = 'Concatenation' s3 = ''.join[[s1, s2]] print[s3]
Output:
StringConcatenation
The join[]
method also allows you to specify a delimiter when
concatenating strings. For example:
Code language: PHP [php]
s1 = 'String' s2 = 'Concatenation' s3 = ' '.join[[s1, s2]] print[s3]
Output:
Code language: JavaScript [javascript]
String Concatenation
In this example, we use the join[]
method to concatenate strings delimited by a space.
The following example use the join[]
method to concatenate strings delimited by a comma:
s1, s2, s3 = 'Python', 'String', 'Concatenation' s = ','.join[[s1, s2, s3]] print[s]
Code language: PHP [php]
Output:
Code language: JavaScript [javascript]
Python,String,Concatenation
5] Concatenating strings using the %-formatting
String objects have the built-in % operator that allows you to format strings. Also, you can use it to concatenate strings. For example:
Code language: PHP [php]
s1, s2, s3 = 'Python', 'String', 'Concatenation' s = '%s %s %s' % [s1, s2, s3] print[s]
Output:
Code language: JavaScript [javascript]
Python String Concatenation
In this example, Python substitutes a %s
in the literal string by corresponding string variable in the tuple that follows the %
operator.
6] Concatenating strings using the format[] method
You can use the format[]
method to concatenate multiple strings into a string. For example:
Code language: PHP [php]
s1, s2, s3 = 'Python', 'String', 'Concatenation' s = '{} {} {}'.format[s1, s2, s3] print[s]
Output:
Code language: JavaScript [javascript]
Python String Concatenation
In this example, you use the {}
in the string literal and pass the string that you want to concatenate to the format[]
method. The format[]
method substitutes the {}
by the corresponding string argument.
7] Concatenating strings using f-strings
Python 3.6 introduced the f-strings that allow you to format strings in a more concise and elegant way.
And you can use the f-strings to concatenate multiple strings into one. For example:
Code language: PHP [php]
s1, s2, s3 = 'Python', 'String', 'Concatenation' s = f'{s1} {s2} {s3}' print[s]
Output:
Code language: JavaScript [javascript]
Python String Concatenation
Which method should you use to concatenate strings
Even though there’re multiple ways to concatenate strings in Python, it’s recommended
to use the join[]
method, the +
operator, and f-strings to concatenate strings.
Did you find this tutorial helpful ?