W3docs

Concatenate Strings

Learn every way to concatenate strings in Python: +, +=, join(), f-strings, and * repetition — with examples, gotchas, and performance tips.

String concatenation is the process of joining two or more strings into one. Python offers several approaches — from the simple + operator to the efficient join() method — and choosing the right one matters for readability and performance.

This chapter covers:

  • The + and += operators
  • Joining a list of strings with join()
  • Embedding values with f-strings
  • Repeating a string with *
  • Concatenating non-string values (and the common TypeError gotcha)
  • When to use join() instead of +

Related chapters: Python Strings · Modify Strings · Format Strings · String Methods

Using the + Operator

The + operator is the most direct way to join two strings. Python creates a new string that contains all the characters of both operands in order.

Concatenate two strings with +

python— editable, runs on the server
Hello World

You can chain as many strings as you need in a single expression:

greeting = "Good" + " " + "morning" + ", " + "Python!"
print(greeting)
Good morning, Python!

Gotcha — only strings allowed. The + operator does not automatically convert other types. Trying to concatenate a string and a number raises a TypeError:

age = 30
# This raises TypeError: can only concatenate str (not "int") to str
# message = "I am " + age + " years old."

# Correct: convert the number to a string first
message = "I am " + str(age) + " years old."
print(message)
I am 30 years old.

Always call str() on a non-string value before using +.

Using the += Operator

The += operator appends a string to an existing variable. It is shorthand for variable = variable + new_string and is handy when you build a string step by step.

Build a sentence incrementally with +=

python— editable, runs on the server
Hello, World!

A common use-case is accumulating lines inside a loop:

words = ["one", "two", "three"]
result = ""
for word in words:
    result += word + " "
print(result.strip())
one two three

Note on performance. Repeated += inside a loop creates a new string object on every iteration. For small lists this is fine, but for large collections join() (see below) is significantly faster.

Using join() to Concatenate a List of Strings

str.join(iterable) joins all the strings in an iterable, placing the string it is called on between each pair. It is the idiomatic Python way to build a string from a collection.

Join words with a space separator

python— editable, runs on the server
Hello World

The separator can be anything — a comma, a newline, or even an empty string:

letters = ["P", "y", "t", "h", "o", "n"]

print(", ".join(letters))   # comma-separated
print("".join(letters))     # no separator — merges into one word
print("\n".join(letters))   # one letter per line
P, y, t, h, o, n
Python
P
y
t
h
o
n

Why prefer join() over + in loops?

Each + call allocates a brand-new string. join() calculates the total length once, allocates memory once, and copies all parts in a single pass — making it O(n) instead of O(n²) for large inputs.

# Slow for large collections
parts = ["a"] * 10_000
result = ""
for p in parts:
    result += p   # 10,000 allocations

# Fast — single allocation
result = "".join(parts)

Using f-Strings to Concatenate Values

f-strings (available since Python 3.6) let you embed variables and expressions directly inside a string literal without any explicit + or str() calls.

first_name = "Ada"
last_name  = "Lovelace"
birth_year = 1815

bio = f"{first_name} {last_name} was born in {birth_year}."
print(bio)
Ada Lovelace was born in 1815.

f-strings are often cleaner than + when you mix several variables with literal text. See the Format Strings chapter for the full f-string syntax and formatting options.

Repeating a String with *

The * operator repeats a string a given number of times, which is a concise alternative to a concatenation loop.

line = "-" * 20
print(line)

echo = "ha" * 3
print(echo)
--------------------
hahaha

Multiline String Concatenation

Python automatically joins adjacent string literals that appear on consecutive lines inside parentheses — no + needed. This is useful for long hard-coded strings.

message = (
    "This is the first part. "
    "This is the second part. "
    "And this is the third."
)
print(message)
This is the first part. This is the second part. And this is the third.

You can also break a long concatenation across lines with a backslash:

long_url = "https://example.com/products" \
           "?category=books" \
           "&sort=price"
print(long_url)
https://example.com/products?category=books&sort=price

Choosing the Right Method

SituationBest choice
Joining two or three literals+
Building a string in a loopjoin()
Mixing variables with textf-string
Joining a list or any iterablejoin()
Repeating a string N times*
Long hard-coded stringsAdjacent literals in ()

For more string operations, see Modify Strings, Slicing Strings, and String Methods.

Practice

Practice
Which of the following are correct ways to concatenate strings in Python?
Which of the following are correct ways to concatenate strings in Python?
Was this page helpful?