Tutorial

Python String Concatenation: Techniques, Examples, and Tips

Comprehensive, original guide to python string concatenation: +, +=, join(), format(), f-strings, performance tips, and best practices.

Drake Nguyen

Founder · System Architect

3 min read
Python String Concatenation: Techniques, Examples, and Tips
Python String Concatenation: Techniques, Examples, and Tips

Introduction

python string concatenation is a common task when building text output, file paths, or messages. This guide explains practical techniques—operator concatenation, join(), format(), and f-strings—when to use each, and performance considerations so you can choose the right approach for your code.

Common methods to concatenate strings in Python

  • Using the + operator and +=
  • Using str.join() to concatenate sequences
  • Using % formatting
  • Using str.format()
  • Using Python f-string (literal string interpolation)

Using + and += operators

Using the + operator is straightforward for combining a few strings:

s1 = "Hello"
s2 = "World"
res = s1 + " " + s2
print(res)  # Hello World

The += operator appends to an existing string but creates a new string object each time, so it is fine for small tasks but not recommended inside large loops because strings are immutable in Python.

Using str.join() (best for lists and separators)

When you have a list or other sequence of strings, use join(). It's efficient because it computes the final size and builds the result once:

words = ["Python", "is", "powerful"]
line = " ".join(words)  # 'Python is powerful'
print(line)

# join with a custom separator
csv = ",".join(["one", "two", "three"])  # 'one,two,three'
print(csv)

Using % operator

The older % formatting still works and can be handy for simple substitutions:

s = "%s %s" % ("Hello", "World")
print(s)  # Hello World

Using str.format()

format() is flexible for named or positional fields and is useful when you need more control over formatting:

name = "Alex"
age = 30
s = "{n} is {a} years old".format(n=name, a=age)
print(s)  # Alex is 30 years old

Using f-strings (Python 3.6+)

F-strings are concise and often faster than format() for formatted text. They call str() on expressions automatically:

name = "Alex"
age = 30
s = f"{name} is {age} years old"
print(s)

Concatenating strings and non-strings

To concatenate a string with an integer or other object, convert the non-string to str() or use formatting/f-strings which handle conversion automatically:

n = 5
# using str()
print("Count: " + str(n))
# using f-string
print(f"Count: {n}")

Concatenation in loops: best practice

Appending repeatedly with += inside a loop is inefficient because it repeatedly allocates new string objects. Prefer collecting parts and joining once:

parts = []
for i in range(1000):
    parts.append(str(i))
result = ",".join(parts)
Tip: For large or repeated concatenation, build a list of fragments and use join() to reduce memory churn.

Performance and memory considerations

General guidance on python string concatenation performance:

  • join() — most efficient when combining many strings or concatenating a list of strings.
  • f-strings — excellent for readable, formatted output and competitive performance for a few values.
  • format() and % — useful for complex formatting; slightly slower than f-strings in many cases.
  • + and += — OK for small, infrequent concatenations; avoid in large loops because strings are immutable.

Practical examples

Concatenate list of strings into a single string

items = ["red", "green", "blue"]
line = " | ".join(items)  # 'red | green | blue'
print(line)

Build a file path (use os.path.join for portability)

import os
folder = "documents"
file = "report.txt"
path = os.path.join(folder, file)  # uses correct separator for OS
print(path)

Formatting with f-strings vs format()

user = "Sam"
score = 95
print(f"{user} scored {score}")
print("{} scored {}".format(user, score))

FAQs

How to concatenate strings in Python?

Use + for simple cases, join() for lists or many parts, and f-strings or format() when you need formatting or conversion.

What is the most efficient way to concatenate strings in Python?

For multiple strings, join() is typically the most efficient. For readable formatted output with a few values, f-strings are both convenient and performant.

How to concatenate strings with a separator?

Use the join() method with the separator as the string on which you call join, e.g. ",".join(list_of_strings).

Is + or join() faster?

join() outperforms + when concatenating many strings or a list. For a small fixed number of strings, + is acceptable but can be less efficient as the number grows.

Conclusion

Choosing the right method for python string concatenation depends on your needs: readability, formatting, or performance. Use join() for lists and repeated concatenation, prefer f-strings for inline formatted text, and avoid += inside large loops. These simple practices help keep your code efficient and maintainable.

Stay updated with Netalith

Get coding resources, product updates, and special offers directly in your inbox.