Master String Concatenation in Python

I was working on a Python project where I had to merge multiple strings to create a formatted message for customer notifications. The challenge was simple: I needed to combine names, city names, and order details into one clean string.

I’ve noticed that beginners often get confused about the right way to concatenate strings in Python. The good news is that there are multiple easy ways to do this, each with its own strengths.

In this tutorial, I’ll walk you through five simple methods to concatenate strings in Python, along with practical examples and my personal insights.

String Concatenation in Python

String concatenation is a way to combine more than one string. Let’s see different ways to concatenate strings in Python using the comma “,” + operator, join() method, and % operator.

Method 1: Python String Concatenation Using Comma

A comma is an approach to concatenating multiple strings together. Here, a comma acts as a single white space, which means you can take more than one string and include the comma to combine those strings.

string1 = "Software"
string2 = "Engineer"

# using the comma to concatenate two strings together
print(string1, string2)

You can see the output in the screenshot below.

Python String Concatenation using Comma

From the output, two strings, “string1” and “string2”, are concatenated using the comma “,” within the print() function. As a result, it returns a string like this: “Software Engineer”. Remember, you can combine any data type using the comma, but here I have shown how to combine strings only.

Method 2: String Concatenation Using “+” Operator

You can use the plus “+” operator to concatenate strings together. Simply add two strings as you add the two numbers, like this: 2 + 2.

For example, to concatenate string1 and string2, you can use the code below.

string1 = "Software "
string2 = "Engineer"

# using the "+" operator to concatenate two strings together
string3 = string1 + string2
print(string3)

You can see the output in the screenshot below.

Concatenate Strings in Python using + Operator

From the output, two strings are concatenated together using the plus “+” operator like this: string1+string2. Using the “+” operator, you can combine any number of Python strings.

Method 3: Python Concatenate Strings Using join() Method

The join() method in Python is a string method that combines multiple strings based on the string separator.

The syntax is given below.

str.join(iterable)

Where, here, consider the str as a separator, the join() method takes an iterable object, which can be a list, tuple, dictionary, etc.

  • Remember, the join() method’s return type is a concatenated string; if you specify non-string values in the join() method, it raises an error.

For example, look below at how the join() method concatenates the strings.

string1 = "Generative"
string2 = "AI"

# using the join() function to concatenate two strings together
string3 = " ".join([string1,string2])
print(string3)

You can see the output in the screenshot below.

Python Concatenate Strings using join() Method

Look at the above output, where two strings are passed as a list [string1,string2] to the join() method, which is called on the string ” ” (these two double quotes act as a separator here).

The concatenated string returned by the join() method is stored in the variable string3. As a result, when you print the variable, it shows the concatenated string as Generative AI.

Method 4: String Concatenation in Python Using “%” Operator

The percent “%” operator formats the string within the print() function, so it can also be used for string concatenation.

For example, to combine string1 and string2, you can use the “%” operator as shown below.

string1 = "Generative"
string2 = "AI"

# using the "%" to concatenate two strings together
print("% s % s" % (string1, string2))

You can see the output in the screenshot below.

String Concatenation in Python using % Operator

From the output, you can see that the “%” operator is used within the print() function to concatenate string1 and string2 together. From the above different methods, I hope that you understand how to concatenate strings together.

Real-World Example – Create a Personalized Email Message

Let’s combine everything we’ve learned into a practical example.

Imagine you’re sending personalized emails to customers in the USA. Here’s how you can generate a message using f-strings:

# Example: Real-world use case – personalized email message

first_name = "Emily"
last_name = "Clark"
city = "Dallas"
state = "Texas"
order_id = 45892

email_message = (
    f"Dear {first_name} {last_name},\n\n"
    f"Thank you for your recent order #{order_id}.\n"
    f"Your package will be shipped soon to {city}, {state}.\n\n"
    f"Best regards,\n"
    f"The PythonShop Team"
)

print(email_message)

This example shows how string concatenation can be used in real-world Python applications like automated messaging or reporting systems.

Conclusion

When it comes to concatenating strings in Python, there’s no single “best” method; it depends on your use case. For quick, simple joins, the + operator works fine. For cleaner and more readable code. And for large-scale concatenation, join() is the most efficient.

Every method has its pros and cons, but you can understand each method at a deeper level.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.