In this Python tutorial, we will discuss how to concatenate strings in Python.
In Python, we can concatenate strings using 8 different methods. Now, all these methods are listed below.
- Using comma (,)
- Using the + operator
- Using concatenating literal strings
- Using the join() method
- Using the += operator
- Using the format() function
- Using f-string
- Using %-formatting
How to concatenate strings in python
Now, let us see how to concatenate strings in python, how to concatenate two strings in python. Also, can we add two strings in python?
Yes, we can add or concatenate strings. There are multiple ways to concatenate strings. See below for all the approaches
Method 1: How to concatenate strings in python using comma (,)
One of the basic ways to concatenate the strings in Python is by using the comma (,).
Here is an example of using comma (,) to concatenate strings in Python.
# Declaring string type variables
myvar1 = "United"
myvar2 = "States"
# Using comma (,) to concatenate both strings
print(myvar1, myvar2)
In the example, we used two string-type variables myvar1 and myvar2. And to concatenate them, we specified both the variable in the print() function separated by a comma (,).
Here is the result of the above Python program.
United States
Read: Python string formatting
Method 2: How to concatenate strings in python using the + operator
Another common way to concatenate strings in Python is by using the + operator.
An example of using the + operator to concatenate strings is given below.
# Declaring string type variables
myvar1 = "United"
myvar2 = "States"
# Using + operator to concatenate both strings
concatenated_string = myvar1 + ' ' + myvar2
# Printing result
print(concatenated_string)
Here we defined 2 string-type variables and then we used the plus (+) operator to concatenate the strings. However, we also added an empty string value between both the variables using plus (+) operator.
Once we execute the Python program, we will get the following result.
United States
Read: Python compare strings
Method 3: How to concatenate strings in python using concatenating literal strings
In Python, we can concatenate literal strings by simply placing them next to each other, without any operator or function.
Let us look at an example of this in Python.
# Concatenating literal strings
myvar = "New" " " "Zealand"
# Printing result
print(myvar)
In the above example, we used the concept of concatenating literal strings on “New” and “Zealand” strings together.
The result of the above Python program is given below.
New Zealand
Read: Python split string by space
Method 4: How to concatenate strings in python using the join() method
The join() is a method in Python that allows concatenating a sequence of strings into a single string using a specified separator.
Here is an example of using the join() method to concatenate Python Strings.
# Declaring string type variables
myvar1 = "United"
myvar2 = "States"
# Using join() to concatenate both strings
concatenated_string = " ".join([myvar1, myvar2])
# Printing result
print(concatenated_string)
In the example, we are concatenating the myvar1 string with myvar2 using the join() method. However, we also specified
United States
Read: Python find number in String
Method 5: How to concatenate strings in python using the += operator
The += is generally used to add values and calculate the sum. However, we can use the + operator on both strings and integers, we can use the += operator on strings to concatenate them.
An example of using the += operator on strings is given below.
# Declaring string type variables
myvar1 = "United"
myvar2 = "Kingdom"
# Using += operator to concatenate both strings
myvar1 += ' ' + myvar2
# Printing result
print(myvar1)
In this example, we are concatenating the United and Kingdom strings using the += operator. The output of the above Python program is also shown below.
United Kingdom
Read: Find first number in string in Python
Method 6: How to concatenate strings in python using the format() function
The format() is a built-in function in Python utilized to format strings. With this function, we used the placeholders as curly brackets, and the format() function replaces the given string with the placeholders.
# Declaring string type variables
myvar1 = "United"
myvar2 = "States"
# Using format() to concatenate both strings
concatenated_string = "{} {}".format(myvar1, myvar2)
# Printing result
print(concatenated_string)
In the example, we concatenated the United string with States using the format() function.
United States
Read: How to trim a string in Python
Method 7: How to concatenate strings in python using f-string
In Python, f-string is short for formatted string literal and it is another way to format strings. This method includes expressions inside curly braces {} that are evaluated and replaced with their values at runtime.
Here is an example of using the f-string to concatenate 2 strings.
# Declaring string type variables
myvar1 = "United"
myvar2 = "Kingdom"
# Using f-string to concatenate both strings
concatenated_string = f"{myvar1} {myvar2}"
# Printing result
print(concatenated_string)
Once we execute the above Python program, we will get the following result.
United States
Read: Python 3 string methods
Method 8: How to concatenate strings in python using %-formatting
Just like format() function % formatting is another way to format string in Python. In this method, we use %s as the placeholder and it will replace the given string with the placeholder.
Here is an example of this approach in Python.
# Declaring string type variables
myvar1 = "United"
myvar2 = "Kingdom"
# Using %-formatting to concatenate both strings
concatenated_string = "%s %s" % (myvar1, myvar2)
# Printing result
print(concatenated_string)
Here is an example where we are concatenating 2 string values together to form the United Kingdom as the result.
You may also like to read the following Python tutorials.
Conclusion
So, in this Python tutorial, we understood how to concatenate strings in Python. Moreover, we have covered 8 different methods to concatenate Python Strings.
Here is the list of 8 methods that we have covered in this tutorial.
- Using comma (,)
- Using the + operator
- Using concatenating literal strings
- Using the join() method
- Using the += operator
- Using the format() function
- Using f-string
- Using %-formatting
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.