- Published on
Concatenate strings in python
Table of Contents
In Python, a string is a sequence of characters enclosed within quotes that can be represented as a text. Concatenating strings is the process of joining/adding one string to another. It is important when working with two or more separate strings and combining them into one a much larger string.
In Python String Concatenation can be done using many ways. In This tutorial are going to explore the different ways to concatenate strings in a python program. We can perform string concatenation in python using following ways:
Using + operator
Using join() method
Using format() function
Using f-string (Literal String Interpolation)
String Concatenation using + Operator
+
Operator is one of the easiest ways of string concatenation. This operator can be used to add multiple strings together just by adding them like numbers
Here is an example.
string_one = "Hellow"
string_two = "World"
string_three = x + y
print(string_three) # ποΈ HellowWorld
In the code above, we created two variables string_one
and string_two
both containing strings Hellow
and World
and a third variable string_three
which combines the two variables we created initially. But the problem is there is no space between the two words in order to achieve this we will have to write it as "Hellow" + " " + "World"
. When concatenating strings with the + operator the variables must be strings Otherwise We can use str()
function to get the string representation of an object. Let's see how to concatenate a string to an integer.
x = "Hellow"
y = 3
z = x + str(y)
print(v3) # ποΈ Hellow3
String concatenation using join() function
The other way to concatenate strings is using the join method. The join()
method is used to concatenate strings with a separator or without separator . It's useful when we have a sequence of strings, for example a list or tuple of strings. If you want to add a separator like space or comma, then use join()
function without an empty string.
Here is an example
my_list = ['a', 'b', 'c']
# β
sum list of strings
# without a separator
my_str = ''.join(my_list)
print(my_str) # ποΈ 'abc'
# with a space separator
my_str_2 = ' '.join(my_list)
print(my_str_2) # ποΈ 'a b c'
# with comma separator
my_str_3 = ', '.join(my_list)
print(my_str_3) # ποΈ 'a, b, c'
String Concatenation using format function
You can use string.format()
method to do simple positional formatting, it can be used for string concatenation too. Itβs useful when we want to concatenate strings and perform simple formatting.
string_one = 'Hello'
string_two = 'World'
string_three = "{}-{}".format(s1, s2)
print('String Concatenation using format() =', s3)
# ποΈ 'String Concatenation using format() = Hello-World'
String Concatenation using f-string
Are Also called formatted string literals f-strings are string literals. To use them we need to add f
at the beginning and curly braces containing expressions that will be replaced with their values. Itβs a new way to format strings and introduced Python 3.6
string_one = 'Hellow'
string_two = 'World'
string_three = f'{s1} {s2}'
print('String Concatenation using f-string =', string_three)
# ποΈ 'String Concatenation using f-string = Hellow World'
Conclusion
In this tutorial you have learned how string formatting can be done in several ways using python. And you have seen four ways to string concatenation in python using the + operator using the join method using the format method the finally using f-string Note that f-string can only be used in Python 3.6 or above versions.