Efficient String Concatenation in Python - Real Python
Efficient String Concatenation in Python - Real Python
Table of Contents
Doing String Concatenation With Python’s Plus Operator (+)
Efficiently Concatenating Many Strings With .join() in Python
Doing Repeated Concatenation With the Star Operator (*)
Exploring Other Tools for String Concatenation
Taking Advantage of String Literal Concatenation
Concatenating Strings With StringIO
Using print() to Concatenate Strings
Conclusion
Remove ads
String concatenation is a common operation in programming. It involves joining two or more strings to create a
single new string. You’ll find several tools and techniques for concatenating strings in Python, each with its own pros
and cons.
In this tutorial, you’ll go through the most common tools and techniques for concatenating strings. You’ll also code
examples for each of them, which will help you choose the best option for your specific problems.
To get the most out of this tutorial, you should have a basic understanding of Python, especially its built-in string data
type.
https://realpython.com/python-string-concatenation/ 1/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Free Download: Click here to download your sample code to learn how to efficiently concatenate strings in
Python.
Python >>>
Using the concatenation operator to join two strings provides a quick solution for concatenating only a few strings.
For a more realistic example, say you have an output line that will print an informative message based on specific
criteria. The beginning of the message might always be the same. However, the end of the message will vary
depending on different criteria. In this situation, you can take advantage of the concatenation operator:
Python >>>
>>> age_group(29)
You are an Adult!
>>> age_group(14)
You are an Adolescent!
>>> age_group(68)
You are in your Golden Years!
In the above example, age_group() prints a final message constructed with a common prefix and the string resulting
from the conditional statement. In this type of use case, the plus operator is your best option for quick string
concatenation in Python.
The concatenation operator has an augmented version that provides a shortcut for concatenating two strings
together. The augmented concatenation operator (+=) has the following syntax:
Python
string += other_string
This expression will concatenate the content of string with the content of other_string. It’s equivalent to saying
string = string + other_string.
Here’s a short example of how the augmented concatenation operator works in practice:
https://realpython.com/python-string-concatenation/ 2/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Python >>>
In this example, every augmented assignment adds a new syllable to the final word using the += operator. This
concatenation technique can be useful when you have several strings in a list or any other iterable and want to
concatenate them in a for loop:
Python >>>
Inside the loop, you use the augmented concatenation operator to quickly concatenate several strings in a loop. Later
you’ll learn about .join(), which is an even better way to concatenate a list of strings.
Python’s concatenation operators can only concatenate string objects. If you use them with a different data type, then
you get a TypeError:
Python >>>
The concatenation operators don’t accept operands of different types. They only concatenate strings. A work-around
to this issue is to explicitly use the built-in str() function to convert the target object into its string representation
before running the actual concatenation:
Python >>>
By calling str() with your integer number as an argument, you’re retrieving the string representation of 42, which you
can then concatenate to the initial string because both are now string objects.
Note: Python’s f-strings provide a great tool for string manipulation. They allow you to put values of different
types into existing strings without the need for explicit conversion.
Python >>>
Note how 42 gets inserted into the target string automatically without complaints about the data type. In this
tutorial, you won’t learn about f-strings because they’re a string interpolation tool rather than a string
concatenation one.
https://realpython.com/python-string-concatenation/ 3/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
To learn more about f-strings, check out Python 3’s f-strings: An Improved String Formatting Syntax (Guide).
String concatenation using + and its augmented variation, +=, can be handy when you only need to concatenate a few
strings. However, these operators aren’t an efficient choice for joining many strings into a single one. Why? Python
strings are immutable, so you can’t change their value in place. Therefore, every time you use a concatenation
operator, you’re creating a new string object.
This behavior implies extra memory consumption and processing time because creating a new string uses both
resources. So, the concatenation will be costly in two dimensions: memory consumption and execution time.
Fortunately, Python has an efficient tool for you to deal with concatenating multiple strings. That tool is the .join()
method from the str class.
Remove ads
Python >>>
In this example, you call .join() on a whitespace character, which is a concrete object of the built-in str class
expressed as a literal. This character is inserted between the strings in the input list, generating a single string.
The .join() method is cleaner, more Pythonic, and more readable than concatenating many strings together in a loop
using the augmented concatenation operator (+=), as you saw before. An explicit loop is way more complex and
harder to understand than the equivalent call to .join(). The .join() method is also faster and more efficient
regarding memory usage.
Unlike the concatenation operators, Python’s .join() doesn’t create new intermediate strings in each iteration.
Instead, it creates a single new string object by joining the elements from the input iterable with the selected
separator string. This behavior is more efficient than using the regular concatenation operators in a loop.
It’s important to note that .join() doesn’t allow you to concatenate non-string objects directly:
Python >>>
When you try to join non-string objects using .join(), you get a TypeError, which is consistent with the behavior of
concatenation operators. Again, to work around this behavior, you can take advantage of str() and a generator
expression:
Python >>>
The generator expression in the call to .join() converts every number into a string object before running the
concatenation and producing the final string. With this technique, you can concatenate objects of different types into
a string.
https://realpython.com/python-string-concatenation/ 4/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Python
string = string * n
string *= n
The repetition operator takes two operands. The first operand is the string that you want to repeat in the
concatenation, while the second operand is an integer number representing how many times you want to repeat the
target string. The augmented syntax is equivalent to the regular one but shorter.
A common example of using this concatenation tool is when you need to generate a separator string to use in tabular
outputs. For example, say you’ve read a CSV file with information about people into a list of lists:
Python >>>
>>> data = [
... ["Name", "Age", "Hometown"],
... ["Alice", "25", "New York"],
... ["Bob", "30", "Los Angeles"],
... ["Charlie", "35", "Chicago"]
... ]
The first row of your list contains the table headers. Now you want to display this info in a table. You can do something
like the following:
Python >>>
>>> display_table(data)
Name |Age |Hometown
--------|--------|--------
Alice |25 |New York
Bob |30 |Los Angeles
Charlie |35 |Chicago
In this example, you use the * operator to repeat the string "-" as many times as defined in max_len, which holds the
number of characters in the longest table header. The loop prints the data in a tabular format. Note how the
conditional statement prints a separation line between the headers and the actual data.
Remove ads
In the following sections, you’ll learn about a few additional techniques and tools that you can use for string
concatenation.
https://realpython.com/python-string-concatenation/ 5/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Python >>>
>>> message = "To: " "The Python Community" " -> " "Welcome Folks!"
>>> message
'To: The Python Community -> Welcome Folks!'
In these examples, you can see that Python automatically merges multiple strings into a single one when you place
them side by side. This feature is known as string literal concatenation, and it’s documented as an intentional
behavior of Python. Note that you can even define variables using this feature.
For example, say that you need to use double quotes and apostrophes in the same string:
Python >>>
>>> phrase = (
... "Guido's talk wasn't named " # String with apostrophes
... '"Feeding CPython to ChatGPT"' # String with double quotes
... )
>>> print(phrase)
Guido's talk wasn't named "Feeding CPython to ChatGPT"
In this example, you take advantage of string literal concatenation to add comments to different parts of your string
and to escape double and single quotes within your final string.
This feature may seem neat at first glance. However, it can be a good way to shoot yourself in the foot. For example,
say that you’re writing a list of strings and accidentally forget an intermediate comma:
Python >>>
>>> hobbies = [
... "Reading",
... "Writing",
... "Painting",
... "Drawing",
... "Sculpting" # Accidentally missing comma
... "Gardening",
... "Cooking",
... "Baking",
... ]
>>> hobbies
[
'Reading',
'Writing',
'Painting',
'Drawing',
'SculptingGardening',
'Cooking',
'Baking'
]
When typing your list, you accidentally missed the comma that separates "Sculpting" from "Gardening". This mistake
introduces a subtle bug into your programs. Because Python doesn’t find the separating comma, it triggers automatic
string literal concatenation, joining both items in a single string, 'SculptingGardening'. This type of error may pass
https://realpython.com/python-string-concatenation/ 6/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
In general, to avoid surprises in your code, you should use explicit string concatenation with + or += whenever you’re
working with long strings or strings that contain escape sequences.
To concatenate strings with StringIO, you first need to import the class from the io module. Then you can use the
.write() method to append individual strings to the in-memory buffer:
Python >>>
Here, you’ll quickly note that a bunch of numbers appear on your screen between operations on the StringIO object.
These numbers represent the bytes written or retrieved from the object in each writing or reading operation.
In the above example, you’ve used a finite data stream represented by a list of strings. If you need to work with a
potentially infinite data stream, then you should use a while loop instead. For example, here’s a small script that takes
words from the user and concatenates them into a sentence:
Python
# sentence.py
sentence = StringIO()
while True:
word = input("Enter a word (or './!/?' to end the sentence): ")
if word in ".!?":
sentence.write(word)
break
if sentence.tell() == 0:
sentence.write(word)
else:
sentence.write(" " + word)
This script grabs the user’s input using the built-in input() function. If the input is a period, an exclamation point, or a
question mark, then the loop breaks, terminating the input. Then you check if the buffer is empty by using the .tell()
method. Depending on this check, the statement adds the current word only or the word with a leading whitespace.
https://realpython.com/python-string-concatenation/ 7/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Shell
$ python sentence.py
Enter a word (or './!/?' to end the sentence): Hello,
Enter a word (or './!/?' to end the sentence): welcome
Enter a word (or './!/?' to end the sentence): to
Enter a word (or './!/?' to end the sentence): Real
Enter a word (or './!/?' to end the sentence): Python
Enter a word (or './!/?' to end the sentence): !
The concatenated sentence is: Hello, welcome to Real Python!
Cool! Your script works nicely! It takes words at the command line and builds a sentence using StringIO for string
concatenation.
Using StringIO to concatenate strings can be an excellent alternative to using the concatenation operators. This tool
is handy when you need to deal with a large or unknown number of strings. StringIO can be pretty efficient because it
avoids creating intermediate strings. Instead, it appends them directly to the in-memory buffer, which can give you
great performance.
StringIO also provides a consistent interface with other file-like Python objects, such as those that open() returns.
This means that you can use the same methods for reading and writing data with StringIO as you would with a
regular file object.
Remove ads
Python >>>
>>> print(*words)
Hello, World! I am a Pythonista!
In these examples, you call print() with an iterable of strings as an argument. Note that you need to use the
unpacking operator (*) to unpack your list into multiple separate string objects that will work as individual arguments
to print().
The print() function takes a sep argument that defaults to a whitespace. You can use this argument to provide a
custom separator for the concatenation. In the second example, you use the newline (\n) escape sequence as a
separator. That’s why the individual words get printed one per line.
Another convenient use of print() in the concatenation context is to save the concatenated string to a file. You can do
this with the file argument to print(). Here’s an example:
Python >>>
After running this code, you’ll have output.txt containing your concatenated string, one word per line, because
you’ve used the newline escape sequence as a separator. Note that the file argument takes file-like objects. That’s
why you use the with statement in the example.
https://realpython.com/python-string-concatenation/ 8/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
In this example, the as keyword creates the output variable, which is an alias of the file object that open() returns.
Then print() concatenates the strings in words using the newline string as a separator and saves the result to your
output.txt file. The with statement automatically closes the file for you, releasing the acquired resources.
Conclusion
You’ve learned about various tools and techniques for string concatenation in Python. Concatenation is an essential
skill for you as a Python developer because you’ll definitely be working with strings at some point. After learning the
most common use cases for each tool or technique, you’re now ready to choose the best approach for your specific
problems, empowering you to write more efficient code.
With the knowledge that you’ve gained in this tutorial, you’re now on your way to becoming a better Python
developer with a solid foundation in string concatenation.
Free Download: Click here to download your sample code to learn how to efficiently concatenate strings in
Python.
Mark as Completed
🐍 Python Tricks 💌
Get a short & sweet Python Trick delivered to your inbox every couple of
days. No spam ever. Unsubscribe any time. Curated by the Real Python
team.
Email Address
Leodanis is an industrial engineer who loves Python and software development. He's a self-taugh
Python developer with 6+ years of experience. He's an avid technical writer with a growing numb
of articles published on Real Python and other sites.
https://realpython.com/python-string-concatenation/ 9/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who
worked on this tutorial are:
Philipp
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use?
Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping
out other students. Get tips for asking good questions and get answers to common questions in our
support portal.
Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours”
Live Q&A Session. Happy Pythoning!
https://realpython.com/python-string-concatenation/ 10/11
5/7/23, 9:11 PM Efficient String Concatenation in Python – Real Python
Keep Learning
Remove ads
https://realpython.com/python-string-concatenation/ 11/11