Python concatenation of strings refers to the merging of two or more strings, one after the other. The dictionary defines “concatenation” as the action of linking things together in a series. For instance, the concatenation of strings “Humpty” and “Dumpty” is “HumptyDumpty”. The easiest way to do concatenation in Python is to use the “+” operator.
String Concatenation using the + operator
Consider the following simple program:
It outputs as expected:
Note that there are no spaces between the two strings. If you need a space, you can do:
This produces:
The above code is an example of overloading in action. Recall that “+” means addition when applied to integers (or floats) but when applied to strings it means concatenation. This is why the following code will not work.
It outputs:
You can typecast the integer to string like so:
This will output:
You can also concatenate newline characters to create multiline strings:
This outputs:
String Concatenation using join()
The second way to do string concatenation is using the join() method. The join() method takes an iterable (e.g., a list) and returns the elements of the iterable interspersed with a given character or set of characters.
Here is the same example as above but concatenated using join():
As you can see join is applied as a method over the string “ “ (1 empty space). It takes as arguments a list containing the strings, in this case the strings “Humpty” and “Dumpty”.
You can also join() using other characters, eg:
This yields:
Doing a join() with an empty string, like:
yields:
Finally, join() can be applied to longer strings, not just single characters, like so:
This gives:
String Concatenation using the format() function
The format() function is a very versatile way to print strings in Python and can thus be used for string concatenation as well. The format function is applied on a template and takes as arguments substitutions for the placeholders in the template. Here is the same Humpty Dumpty example realized using the format() function:
This produces as expected:
Each of the curly braces denotes one placeholder and therefore format must use as many arguments as there are placeholders. If you do:
You will get an error such as:
indicating that there are 3 arguments and index 2 (i.e., the 3rd argument) is out of range in the provided statement.
format() is a very versatile function and you can pass not just strings but also numbers, like so:
This produces:
String Concatenation using the % operator
The final way to do string concatenation in Python is using the % operator. It is actually similar to the format() function in that it is designed to format strings. Here is our Humpty Dumpty example again:
The output is:
The “%s” denote string formatting. If you wanted to print a number, you will use “%d”. For instance:
This yields:
Note that Python is very strict about the order of arguments and the order in which they are used in the string template. If you mix them up:
You get an error:
implying that Python is unable to convert the third argument (string2) into the required integer (%d).
On the other hand, if you do:
You get:
implying Python has treated the two arguments as strings.
Which string concatenation operator to use?
We have learnt how versatile Python is as a language providing many ways to do string concatenation. Depending on your needs one or more of these methods is better. If you just want to concatenate 2 or 3 strings, the “+” operator is easiest. If the strings you would like to concatenate are part of a list or if the interspersing character is more complicated, the join is convenient. If you have important formatting and pretty printing needs, format() or the % operator is the best.
Kodeclik is an online coding academy for kids and teens to learn real world programming. Kids are introduced to coding in a fun and exciting way and are challeged to higher levels with engaging, high quality content.