Python Concatenate Strings: +, f-strings, join, and +=

String concatenation means building one string from pieces: literals, variables, or slices of other strings. Python offers +, f-strings, str.join, and +=. This guide follows a quick answer, each technique, when to prefer which, common mistakes, a cheat sheet, and a short summary.

Tested on: Python 3.13.3; kernel 6.14.0-37-generic.


Quick answer: concatenate strings in Python

For a few pieces, use + or an f-string. For many pieces already in a list, use "".join(parts) (or another separator as the method receiver). For more on older str.format, see Python string format. For appending to an existing variable in a loop, see append string in Python.

python
a, b = "hello", "world"
print(a + " " + b)
print(f"{a} {b}")
print(" ".join([a, b]))
Output

What is string concatenation in Python?

Concatenation produces a new str object; original strings are not mutated (strings are immutable). Each + or slice-based build allocates a new string holding the combined text.

python
s = "py" + "thon"
print(s, len("py") + len("thon") == len(s))
Output

Concatenate strings using + operator

+ joins two strings end to end. It is clear for two or three fragments and avoids a call to join when you do not already have a list.

python
greeting = "Hi, " + "Alex" + "!"
print(greeting)
Output

Add space while concatenating strings

+ does not insert spaces; put them inside the literals or build them explicitly.

python
first = "Jane"
last = "Doe"
print(first + " " + last)
print(first, last)  # print adds a space between arguments
Output

Concatenate string and variable in Python

Use + with every piece as a str, or use an f-string so values are formatted in place.

python
name = "Sam"
age = 30
print("Name: " + name + ", age: " + str(age))
print(f"Name: {name}, age: {age}")
Output

Concatenate multiple strings in Python

Chain +, use "".join((a, b, c)), or build a list and join once. Parentheses help readability on long chains.

python
parts = ("one", "two", "three")
print(", ".join(parts))
Output

Concatenate strings using f-strings

Formatted string literals (f"...") embed expressions in {...}. They are usually the most readable way to mix text and variables in Python 3.6+.

python
user = "dev"
lines = 42
print(f"{user} pushed {lines} lines today")
Output

Concatenate strings using join()

str.join is called on the separator string and takes an iterable of strings. It is the idiomatic way to combine many parts with a delimiter—the complement of Python split string.

Join a list of strings

python
words = ["python", "concatenate", "strings"]
print("".join(words))
print(" ".join(words))
Output

Join strings with separator

python
path_bits = ("home", "user", "doc.txt")
print("/".join(path_bits))
Output

Non-string values in the iterable raise TypeError; convert first (str(x)).


Concatenate strings using += operator

+= on a string name rebinds it to a new string each time (s = s + other). For a few appends it is fine; in a long loop it can be slower than accumulating a list and joining once.

python
msg = "start"
msg += " + middle"
msg += " + end"
print(msg)
Output

+ vs f-string vs join(): which one should you use?

Situation Prefer
Two or three literals or variables + or f-string
Several values with formatting f-string
Many pieces already in a list str.join
Building inside a tight loop list + join, or io.StringIO
Legacy code without f-strings str.format or % (discouraged for new code)

Common mistakes in Python string concatenation

These are the issues that show up most often in code review and in tracebacks: spacing is easy to forget with +, mixing numeric types with + raises TypeError, and growing a string in a loop scales poorly. Each subsection states the symptom, shows a minimal fix, and notes when the cost actually matters.

Missing space between strings

+ glues bytes exactly as written. Two literals "foo" and "bar" become "foobar" unless you insert a space literal, a variable that already contains trailing space, or switch to print(a, b) / an f-string with an explicit space in the template.

python
print("foo" + "bar")   # foobar
print("foo" + " " + "bar")
Output

For dynamic pieces, build spacing into the data (first + " " + last) or use an f-string: f"{first} {last}".

Concatenating string with number

The + operator is only defined between two strings. Integers, floats, and other types must be converted first, or you should skip + entirely and let an f-string or str.format handle the conversion for you.

python
count = 7
# print("items: " + count)  # TypeError
print("items: " + str(count))
print(f"items: {count}")
Output

str(count) is explicit; f"items: {count}" is usually clearer when several values are involved.

Using + inside loops for many strings

Each += on a string creates a new string object and copies the old contents forward, so the work grows like roughly O(n²) when you append many growing pieces. A few iterations is harmless; thousands or millions of rows from a file or database is where join, a list comprehension plus one join, or io.StringIO pays off.

python
# slower pattern for large n
s = ""
for i in range(5):
    s += str(i)

# usually better for many parts
parts = [str(i) for i in range(5)]
t = "".join(parts)
print(s, t)
Output

For small n, either version is readable—choose clarity first. When profiling shows a hot path, collect fragments in a list (or write to io.StringIO) and materialize one final string at the end.


Python string concatenation quick reference table

Technique Example Notes
+ "a" + "b" No auto spaces; both sides must be str
f-string f"{x}{y}" Embeds expressions; readable
join ",".join(xs) xs must be strings
+= s += "x" Repeated use in huge loops can be costly
print print(a, b) Adds spaces between arguments, not true concatenation

Summary

Concatenation builds new strings: use + for tiny joins, f-strings when variables and formatting dominate, and str.join when you have many segments (especially from a list). Add spaces explicitly with + or use print’s multi-argument form for human spacing. Convert numbers with str(...) or f-strings before combining with +. Avoid growing strings with += inside hot loops; batch with join. For broader Python context, see beginner tips for Python.


Frequently Asked Questions

1. What is the fastest way to concatenate many strings in Python?

Collect pieces in a list and call str.join once, or write to io.StringIO; repeated += or + in a tight loop creates many intermediate string objects and is usually slower.

2. How do I concatenate a string and a number in Python?

Convert the number with str(n), use an f-string like f"count={n}", or use format with a field specifier; the + operator does not add str and int directly.

3. When should I use an f-string instead of + for concatenation?

Prefer f-strings when you are building readable text with several variables or expressions; use + for two or three plain literals, and use join when the pieces already live in a list.
Deepak Prasad

R&D Engineer

Founder of GoLinuxCloud with more than 15 years of expertise in Linux, Python, Go, Laravel, DevOps, Kubernetes, Git, Shell scripting, OpenShift, AWS, Networking, and Security. With extensive experience, he excels across development, DevOps, networking, and security, delivering robust and efficient solutions for diverse projects.

  • Go (programming language)
  • Python (programming language)
  • DevOps
  • Computer Security
  • Cloud Computing
  • Kubernetes
  • Linux
  • Ansible (software)