Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How do you concatenate strings in Python?

Python
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 28, 2026
6 min read

Python string concatenation is most often done with the + operator for two or three pieces, with an f-string when you mix text and values, and with "".join(...) when you combine many strings from a list or loop. Strings are immutable in Python, so every concatenation builds a new string object rather than changing an existing one.

This article covers each approach on Python 3.13 and 3.14, shows when each one is the right tool, and explains the common TypeError you get when you try to add a string and a number.

Table of Contents

  • Joining strings with the plus operator
  • Why adding a string and a number fails
  • Building text with f-strings
  • Joining many strings with str.join
    • Why join is better than += in a loop
  • Other ways to concatenate
  • Frequently Asked Questions
    • What is the fastest way to concatenate strings in Python?
    • Can you concatenate a string and an integer?
    • Is += on strings slow?
    • How do you concatenate strings across several lines?
  • Summary

Joining strings with the plus operator

The + operator takes two strings and returns a new string that holds the characters of the left side followed by the characters of the right side.

python
text

The space is its own string, because + adds nothing between the pieces. The augmented form += is a shortcut for text = text + other:

python
text

Two string literals written next to each other are joined by the compiler before the program runs, with no operator at all:

python
text

This only works for literals, not for variables. It is mainly useful for splitting a long literal across lines inside parentheses.

Why adding a string and a number fails

Python does not convert types silently when you use +. Mixing a string with an integer raises a TypeError:

python
text

You can convert the number first with str(), but an f-string is usually cleaner:

python
text

Building text with f-strings

An f-string (formatted string literal) starts with f before the opening quote. Anything inside {} is evaluated as an expression and converted to text. This is the standard way to put values into a sentence.

python
text

After the colon you can add a format specifier such as .1f for one decimal place. The !r conversion applies repr(). Since Python 3.12 you can reuse the same quote type inside the braces and spread an f-string over several lines, which removes most of the old workarounds. For most mixed text-and-value cases, an f-string is easier to read than a chain of + and str() calls.

Joining many strings with str.join

When you have a list, tuple or other iterable of strings, call join() on the separator string:

python
text

Every item must already be a string. If your list has numbers, convert them inside a generator expression:

python
text

Why join is better than += in a loop

Because strings are immutable, result += piece inside a loop can copy the growing string on every pass. CPython has an optimisation that often hides this cost for simple cases, but it is an implementation detail and not guaranteed, and other Python implementations do not have it. The portable pattern is to collect the pieces in a list and join once:

python
text

Other ways to concatenate

MethodExampleBest for
+a + bTwo or three short strings
+=s += xA few appends, not large loops
f-stringf"{a} {b}"Mixing text and values
str.join", ".join(items)Many strings from an iterable
print(a, b, sep="")print("a", "b", sep="")Output only, no new string
str.format"{} {}".format(a, b)Templates stored separately from the values

The older % operator ("%s %s" % (a, b)) still works but f-strings and format() are preferred in new code.

Bytes and strings cannot be mixed either. b"abc" + "def" raises a TypeError; decode the bytes with .decode() first. Lists of characters are joined the same way as lists of words, for example "".join(["a", "b", "c"]) gives "abc".

Frequently Asked Questions

What is the fastest way to concatenate strings in Python?

For many pieces, str.join on a list is the fastest and most portable choice. For two or three strings the difference is negligible, so pick the form that reads best.

Can you concatenate a string and an integer?

Not with +. Convert the integer with str() or put it in an f-string, such as f"Total: {total}".

Is += on strings slow?

In a small number of appends it is fine. In a large loop it can become quadratic on some Python implementations, so collect pieces in a list and call join() once.

How do you concatenate strings across several lines?

Wrap adjacent string literals in parentheses, or use a triple-quoted string. Adjacent literals are joined at compile time.

Summary

Use + for a couple of strings, an f-string when you combine text with values, and "".join(items) when you build a string from many parts. Strings are immutable, so each concatenation creates a new object, and mixing a string with a number requires an explicit conversion.

Share

Similar posts

  • How do you format numbers with f-strings in Python?
  • How do you use f-strings in Python?
  • What are escape sequences in Python strings?
  • Why are strings immutable in Python?
  • How do you check if a string is a number in Python?
  • How do you count occurrences of a substring in Python?
  • How do you check if a string contains a substring in Python?
  • How do you change the case of a string in Python?