Course
Concatenating means obtaining a new string that contains both of the original strings. In Python, there are a few ways to concatenate or combine strings. The new string that is created is referred to as a string object.
Examples of Python String Concatenation
Let's see examples of 5 different methods to concatenate strings in Python.
1. Using the + operator
In order to merge two strings into a single object, you may use the +
operator. This method is straightforward and commonly used.
In the below example, two original strings my_string1
and my_string2
are concatenated. To concatenate them, you use a plus operand to "sum up" both the strings, specifying the space and generating the output, as seen below.
my_string1 = "Awesome day"
my_string2 = "for bikini"
combined_string = my_string1 + " " + my_string2
print(combined_string)
Output:
Awesome day for bikini
2. Using the join() method
The join()
method is another way to concatenate strings, especially useful when you have a list of strings to concatenate.
my_strings = ["Awesome day", "for bikini"]
combined_string = " ".join(my_strings)
print(combined_string)
Output:
Awesome day for bikini
3. Using f-Strings (formatted string literals)
Python 3.6 introduced f-strings, a more readable and efficient way to concatenate strings and include variables within strings.
my_string1 = "Awesome day"
my_string2 = "for bikini"
combined_string = f"{my_string1} {my_string2}"
print(combined_string)
Output:
Awesome day for bikini
4. Using the % operator
The %
operator allows you to format strings and is another method for concatenation, although less commonly used with the introduction of f-strings.
my_string1 = "Awesome day"
my_string2 = "for bikini"
combined_string = "%s %s" % (my_string1, my_string2)
print(combined_string)
Output:
Awesome day for bikini
5. Using the format() method
The format()
method is versatile and can be used for string concatenation and more complex string formatting.
my_string1 = "Awesome day"
my_string2 = "for bikini"
combined_string = "{} {}".format(my_string1, my_string2)
print(combined_string)
Output:
Awesome day for bikini
Conclusion
In Python, there are multiple ways to concatenate strings, each with its own advantages. The +
operator is simple and direct, join()
is efficient for lists, f-strings offer readability and performance, %
provides a classic approach, and format()
offers versatility. Choose the method that best fits your needs and Python version compatibility.
To learn more about Python string manipulation, please see this video from our course, Regular Expressions in Python.
Or check out these tutorials:
FAQs
How do I concatenate a list of strings using a custom separator?
You can use the join()
method with your desired separator.
my_strings = ["Awesome day", "for bikini"]
separator = " - "
combined_string = separator.join(my_strings)
print(combined_string) # Output: Awesome day - for bikini
Is it possible to concatenate strings with other data types like integers or floats directly?
No, you need to convert the other data types to strings first using the str()
function.
my_string = "The price is"
price = 50
combined_string = my_string + " " + str(price) + "$"
print(combined_string) # Output: The price is 50$
How do I concatenate multiple strings stored in a list or a tuple?
Use the join()
method for efficient concatenation.
my_list = ["Awesome day", "for bikini", "at the beach"]
combined_string = " ".join(my_list)
print(combined_string) # Output: Awesome day for bikini at the beach
How can I concatenate strings based on certain conditions?
Use an if
statement to check the conditions before concatenating.
condition = True
my_string1 = "Awesome day"
my_string2 = "for bikini"
if condition:
combined_string = my_string1 + " " + my_string2
else:
combined_string = my_string1
print(combined_string) # Output: Awesome day for bikini
Are there performance differences between using the + operator, join(), f-strings, % operator, and format() for string concatenation?
Yes, there are performance differences. Generally, join()
is the fastest method for concatenating a list of strings, especially in a loop, because it minimizes the creation of intermediate strings. f-strings and format()
are also efficient and provide readability, while the +
operator and %
operator are less efficient for multiple concatenations in loops.
# Example of efficient concatenation in a loop using join()
strings = ["Awesome", "day", "for", "bikini"]
combined_string = " ".join(strings)
print(combined_string) # Output: Awesome day for bikini