HOWTO · Python
How to Append One String to Another in Python
Learn how to append one string to another in Python with the + operator and str.join(), including examples and guidance on choosing between them.
On this page
To append one string to another in Python, use the + operator. Use str.join() when you want to combine an iterable of strings with a chosen separator. Both produce a new string; the right choice depends on whether you are combining a couple of values or a collection.
Append Strings with the + Operator
The + operator concatenates strings from left to right and returns a new string. Add a space or other separator explicitly when the combined text needs one; concatenation does not insert spaces automatically.
Run this example:
first = "Hello"
second = "World"
combined = first + " " + second
print(combined)
Hello World
The space in the expression separates the two values, and print() writes the resulting string. You can also write first += second when updating a variable. That rebinds first to the concatenated result; it does not modify the original string in place, since Python strings are immutable.
For two or a few values, + keeps the expression direct and readable. If you build a long result through repeated concatenation in a loop, each operation creates another string result. Collect the pieces and join them once when that makes the code clearer, especially when the pieces already come from an iterable.
Combine Strings with str.join()
str.join() is called on the separator string, and its single argument is an iterable containing strings. It places the separator between adjacent elements. For example, " ".join(["Hello", "World", "from", "Python"]) combines four words with spaces. Changing the separator changes what appears between the words: "-".join(["red", "green", "blue"]) uses hyphens, while "".join(["Py", "thon"]) adds no separator.
Every element supplied to join() must already be a string. If values include numbers or other types, convert each value first, such as with str(value), or format the values as part of building the collection. A string is itself iterable, so passing a single word directly to join() treats it as a sequence of characters; pass a collection of complete strings when that is what you intend to combine.
Choose Between + and str.join()
Use + for a small, fixed number of strings, such as adding a name to a greeting. It makes the pieces and their order easy to see. Use str.join() when the values are already in a list or another iterable, or when a separator belongs between many pieces. An empty separator is useful when the pieces should meet directly.
Neither choice is universally best for performance. Prefer the form that matches your data and makes the intended separator clear. When combining non-string values, convert or format them deliberately before joining. These rules cover the common cases: + combines string expressions, while str.join() combines an iterable of strings around a separator.