String concatenation is the process of joining two or more strings together to create a single, new string. In Python, there are several ways to accomplish this, depending on your needs and the version of Python you are using.
Here is a detailed breakdown of the most common methods for concatenating strings.
1. Using the + Operator (The Standard Way)
The most basic and intuitive way to combine strings is by using the addition symbol (+). Just as it adds numbers together in math, it adds strings together in Python.
Note: The + operator does not automatically add spaces between the strings. If you want a space, you must explicitly include it.
Python
first_name = "Harry"
last_name = "Potter"
# Concatenating without a space
full_name_no_space = first_name + last_name
print(full_name_no_space) # Output: HarryPotter
# Concatenating with a space in the middle
full_name = first_name + " " + last_name
print(full_name) # Output: Harry Potter
2. Using the += Operator (Appending)
If you already have a string stored in a variable and you want to add more text to the end of it, you can use the += operator. This is very useful when building a long string inside a loop.
Python
message = "Welcome"
message += " to"
message += " Python!"
print(message) # Output: Welcome to Python!
3. Using the .join() Method (Best for Lists)
When you have a list (or any iterable) containing multiple strings and you want to merge them all into one, the .join() method is the most efficient and Pythonic way to do it.
You call .join() on the string you want to use as a separator (like a space, a comma, or a hyphen).
Python
words = ["Python", "is", "awesome"]
# Join with a space
sentence = " ".join(words)
print(sentence) # Output: Python is awesome
# Join with a hyphen
hyphenated = "-".join(words)
print(hyphenated) # Output: Python-is-awesome
4. Using f-strings (The Modern Way)
Introduced in Python 3.6, f-strings (formatted string literals) are the cleanest and most readable way to combine text and variables. You place an f before the opening quotation mark and place any variables directly inside curly brackets {}.
While technically this is “string formatting” rather than pure concatenation, it is the preferred method for building strings out of multiple parts in modern Python.
Python
color = "red"
fruit = "apple"
# It handles spaces naturally based on how you write the sentence
description = f"The {fruit} is the color {color}."
print(description) # Output: The apple is the color red.
⚠️ Common Pitfall: Concatenating Strings and Numbers
Python is strictly typed when it comes to operators. You cannot use the + operator to combine a string and a number (like an integer or a float). Attempting to do so will result in a TypeError.
Python
name = "Alice"
age = 13
# THIS WILL CAUSE AN ERROR:
# profile = name + " is " + age + " years old."
How to fix it:
You must manually convert the number into a string using the str() function before adding it, OR simply use an f-string (which handles the conversion automatically).
Python
# Fix Option 1: Using str()
profile1 = name + " is " + str(age) + " years old."
# Fix Option 2: Using f-strings (Recommended)
profile2 = f"{name} is {age} years old."