Recommended Video Course
Concatenating Strings in Python Efficiently
Table of Contents
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Concatenating Strings in Python Efficiently
Pythonstring concatenation is a fundamental operation that combines multiple strings into a single string. In Python, you can concatenate strings using the+
operator or the+=
operator for appending. For more efficient concatenation of multiple strings, the.join()
method is recommended, especially when working with strings in a list. Other techniques include usingStringIO
for large datasets or theprint()
function for quick screen outputs.
By the end of this tutorial, you’ll understand that:
+
and+=
operators.+=
toappend a string to an existing string..join()
method is used tocombine strings in a list in Python.StringIO
as a container with a file-like interface.To get the most out of this tutorial, you should have a basic understanding of Python, especially its built-instring data type.
Get Your Code:Click here to download the free sample code that shows you how to efficiently concatenate strings in Python.
+
)String concatenation is a pretty common operation consisting of joining two or more strings together end to end to build a final string. Perhaps the quickest way to achieve concatenation is to take two separate strings and combine them with the plus operator (+
), which is known as theconcatenation operator in this context:
>>>"Hello, "+"Pythonista!"'Hello, Pythonista!'>>>head="String Concatenation ">>>tail="is Fun in Python!">>>head+tail'String Concatenation is Fun in 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 willprint 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:
>>>defage_group(age):...if0<=age<=9:...result="a Child!"...elif9<age<=18:...result="an Adolescent!"...elif19<age<=65:...result="an Adult!"...else:...result="in your Golden Years!"...print("You are "+result)...>>>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 theconditional 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. Theaugmented concatenation operator (+=
) has the following syntax:
string+=other_string
This expression will concatenate the content ofstring
with the content ofother_string
. It’s equivalent to sayingstring = string + other_string
.
Here’s a short example of how the augmented concatenation operator works in practice:
>>>word="Py">>>word+="tho">>>word+="nis">>>word+="ta">>>word'Pythonista'
In this example, everyaugmented assignment adds a new syllable to the final word using the+=
operator. This concatenation technique can be useful when you have several strings in alist or any otheriterable and want to concatenate them in afor
loop:
>>>defconcatenate(iterable,sep=" "):...sentence=iterable[0]...forwordiniterable[1:]:...sentence+=(sep+word)...returnsentence...>>>concatenate(["Hello,","World!","I","am","a","Pythonista!"])'Hello, World! I am a Pythonista!'
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 aTypeError
:
>>>"The result is: "+42Traceback (most recent call last):...TypeError:can only concatenate str (not "int") to str>>>"Your favorite fruits are: "+["apple","grape"]Traceback (most recent call last):...TypeError:can only concatenate str (not "list") to str
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-instr()
function to convert the target object into itsstring representation before running the actual concatenation:
>>>"The result is: "+str(42)'The result is: 42'
By callingstr()
with your integer number as an argument, you’re retrieving the string representation of42
, which you can then concatenate to the initial string because both are now string objects.
Note: Python’sf-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.
For example, you can write the above example as follows:
>>>f"The result is:{42}"'The result is: 42'
Note how42
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 astring interpolation tool rather than a string concatenation one.
To learn more about f-strings, check outPython’s F-String for String Interpolation and Formatting.
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 areimmutable, so you can’t change their valuein 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 andexecution time. Fortunately, Python has an efficient tool for you to deal with concatenating multiple strings. That tool is the.join()
method from thestr
class.
.join()
in PythonYou can call the.join()
method on a string object that will work as a separator in the string concatenation process. Given an iterable of strings,.join()
efficiently concatenates all the contained strings together into one:
>>>" ".join(["Hello,","World!","I","am","a","Pythonista!"])'Hello, World! I am a Pythonista!'
In this example, you call.join()
on a whitespace character, which is a concrete object of the built-instr
class expressed as aliteral. 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:
>>>"; ".join([1,2,3,4,5])Traceback (most recent call last):...TypeError:sequence item 0: expected str instance, int found
When you try to join non-string objects using.join()
, you get aTypeError
, which is consistent with the behavior of concatenation operators. Again, to work around this behavior, you can take advantage ofstr()
and agenerator expression:
>>>numbers=[1,2,3,4,5]>>>"; ".join(str(number)fornumberinnumbers)'1; 2; 3; 4; 5'
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.
*
)You can also use the star operator (*
) to concatenate strings in Python. In this context, this operator is known as therepeated concatenation operator. It works by repeating a string a certain number of times. Its syntax is shown below, along with its augmented variation:
string=string*nstring*=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 aCSV file with information about people into a list of lists:
>>>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:
>>>defdisplay_table(data):...max_len=max(len(header)forheaderindata[0])...sep="-"*max_len...forrowindata:...print("|".join(header.ljust(max_len)forheaderinrow))...ifrow==data[0]:...print("|".join(sepfor_inrow))...>>>display_table(data)Name |Age |Hometown--------|--------|--------Alice |25 |New YorkBob |30 |Los AngelesCharlie |35 |Chicago
In this example, you use the*
operator to repeat the string"-"
as many times as defined inmax_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.
Python is a highly flexible and versatile programming language. Even though theZen of Python states thatthere should be one—and preferably only one—obvious way to do it, you’ll often find several options for running a given computation or performing a certain action in Python. String concatenation is no exception to this behavior.
In the following sections, you’ll learn about a few additional techniques and tools that you can use for string concatenation.
Another quick way to concatenate multiple strings in Python is to write the string literals consecutively:
>>>"Hello,"" ""World!"'Hello, World!'>>>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 asstring literal concatenation, and it’s documented as an intentional behavior of Python. Note that you can even define variables using this feature.
Some common use cases for string literal concatenation include:
For example, say that you need to use double quotes and apostrophes in the same string:
>>>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:
>>>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 unnoticed, causing hard-to-debug issues in your code.
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.
StringIO
If you’re working with many strings in a data stream, thenStringIO
may be a good option for string concatenation. This class provides a native in-memoryUnicode container with great speed and performance.
To concatenate strings withStringIO
, you first need to import the class from theio
module. Then you can use the.write()
method to append individual strings to the in-memory buffer:
>>>fromioimportStringIO>>>words=["Hello,","World!","I","am","a","Pythonista!"]>>>sentence=StringIO()>>>sentence.write(words[0])6>>>forwordinwords[1:]:...sentence.write(" "+word)...723212>>>sentence.getvalue()'Hello, World! I am a Pythonista!'
Here, you’ll quickly note that a bunch of numbers appear on your screen between operations on theStringIO
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 awhile
loop instead. For example, here’s a small script that takes words from the user and concatenates them into a sentence:
# sentence.pyfromioimportStringIOsentence=StringIO()whileTrue:word=input("Enter a word (or './!/?' to end the sentence): ")ifwordin".!?":sentence.write(word)breakifsentence.tell()==0:sentence.write(word)else:sentence.write(" "+word)print("The concatenated sentence is:",sentence.getvalue())
This script grabs the user’s input using the built-ininput()
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.
Here’s how this script works in practice:
$pythonsentence.pyEnter a word (or './!/?' to end the sentence): Hello,Enter a word (or './!/?' to end the sentence): welcomeEnter a word (or './!/?' to end the sentence): toEnter a word (or './!/?' to end the sentence): RealEnter a word (or './!/?' to end the sentence): PythonEnter 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 usingStringIO
for string concatenation.
UsingStringIO
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 thatopen()
returns. This means that you can use the same methods for reading and writing data withStringIO
as you would with a regular file object.
print()
to Concatenate StringsYou can also use the built-inprint()
function to perform some string concatenations, especially to concatenate strings for on-screen messages. This function can help you connect strings with a specific separator:
>>>words=["Hello,","World!","I","am","a","Pythonista!"]>>>print(*words)Hello, World! I am a Pythonista!>>>print(*words,sep="\n")Hello,World!IamaPythonista!
In these examples, you callprint()
with an iterable of strings as an argument. Note that you need to use theunpacking operator (*
) to unpack your list into multiple separate string objects that will work as individual arguments toprint()
.
Theprint()
function takes asep
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 ofprint()
in the concatenation context is to save the concatenated string to a file. You can do this with thefile
argument toprint()
. Here’s an example:
>>>words=["Hello,","World!","I","am","a","Pythonista!"]>>>withopen("output.txt","w")asoutput:...print(*words,sep="\n",file=output)...
After running this code, you’ll haveoutput.txt
containing your concatenated string, one word per line, because you’ve used the newline escape sequence as a separator. Note that thefile
argument takesfile-like objects. That’s why you use thewith
statement in the example.
In this example, theas
keyword creates theoutput
variable, which is an alias of the file object thatopen()
returns. Thenprint()
concatenates the strings inwords
using the newline string as a separator and saves the result to youroutput.txt
file. Thewith
statement automaticallycloses the file for you, releasing the acquired resources.
You’ve learned about various tools and techniques forstring 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.
In this tutorial, you’ve learned how to:
+
and+=
.join()
method fromstr
StringIO
, andprint()
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.
Get Your Code:Click here to download the free sample code that shows you how to efficiently concatenate strings in Python.
Now that you have some experience with string concatenation in Python, you can use the questions and answers below to check your understanding and recap what you’ve learned.
These FAQs sum up the most important concepts you’ve covered in this tutorial. Click theShow/Hide toggle beside each question to reveal the answer.
You can concatenate strings in Python with the+
operator by placing it between two strings. This operator, known as the concatenation operator in this context, joins the strings together to form a new one. For example,"Hello, " + "World!"
results in"Hello, World!"
. Note that spaces aren’t added automatically. In this example, a literal space character is included in the first string to separate the two words.
The.join()
method is a string method in Python that efficiently concatenates a list or other iterable of strings into a single string, using a specified separator between each element. For example," ".join(["Hello,", "World!"])
results in"Hello, World!"
, while"-".join(["one", "two", "three"])
results in"one-two-three"
.
This method is more efficient than using+
for concatenating multiple strings because it avoids the creation of intermediate string objects.
No, both the+
operator and the.join()
method require string operands. Using them with non-string types will raise aTypeError
. To concatenate non-string types, you can first convert them to strings usingstr()
, or you can use an f-string for formatting.
TheStringIO
class, from theio
module, provides an in-memory stream for text I/O operations. It allows you to concatenate strings efficiently by writing them into a buffer using the.write()
method. This can be particularly useful when dealing with a large or unknown number of strings, as it avoids creating multiple intermediate string objects.
String literal concatenation is a feature in Python where two or more string literals placed next to each other are automatically concatenated into a single string. While it can be useful for splitting long strings across multiple lines, it can also lead to subtle bugs if commas are accidentally omitted in lists of strings, resulting in unintended concatenation.
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Concatenating Strings in Python Efficiently
🐍 Python Tricks 💌
Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.
AboutLeodanis Pozo Ramos
Leodanis is a self-taught Python developer, educator, and technical writer with over 10 years of experience.
» More about LeodanisMasterReal-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
MasterReal-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
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 andget answers to common questions in our support portal.
Keep Learning
Related Topics:basicsbest-practicespython
Recommended Video Course:Concatenating Strings in Python Efficiently
Related Tutorials:
Already have an account?Sign-In
Almost there! Complete this form and click the button below to gain instant access:
Efficient String Concatenation in Python (Sample Code)