This article will introduce you to simple programming concept that holds immense value in certain situations, that is Python String Concatenation. Following pointers will be covered in this article,
Python string concatenation
If you are new to using the Python language, you may have never heard of the term string concatenation. Be it any programming language you use, there will come a time when you need to merge the contents of two or more strings together, the process of which is known as concatenation in technical terminology. The simplest way to explain this concept is; you take two different strings that are stored in the interpreter and merge them together to achieve a single unified purpose. An example of this action is; there are two different strings hello and world in Python. You make use of the concatenation function and make them one, which is hello world.
Moving on with this article on Python String Concatenation
Concatenation in Python
There are a few major ways to concatenate strings in Python. But first let us get the basics clear; the new string that is formed after concatenation of two separate strings is known as the string object. This is because, at its core Python is an object-oriented language and thus every element in it, be it an already existing or newly created one is referred to as an object.
The easiest way to concatenate two strings in Python is to use the + or plus operator. An example of this as below.
str1 = “Hello” str2 = “World” str1 + str2
The last line in the above code is the concatenation and when the process is executed, a new string is formed.
One of the most important things to note here is the fact that Python cannot concatenate an integer and a string as they are considered to be fundamentally different objects. If you want to concatenate a string and an integer, you will first need to convert either of them into a different object, that is string into integer or integer into a string.
If you try to concatenate a string and integer without converting, an error will be returned on the screen. To understand this better, take a look at the example below.
>>> print ‘red’ + ‘yellow’ Redyellow >>> print ‘red’ * 3 Redredred >>> print ‘red’ + 3 Traceback (most recent call last): File “”, line 1, in TypeError: cannot concatenate ‘str’ and ‘int’ objects >>
Moving on with this article on Python String Concatenation
Formatting a String in Python
Now that you are aware of the basics of concatenating a string in Python, let us discuss more about formatting a string in Python.
In Python, there are two main methods of string interpolation. To understand this better, let us first look at what string interpolation means.
String Interpolation in Python can be simply defined as the process of evaluating a string value that is contained in the interpreter memory as one or more placeholders. In simple terms, string interpolation is a method which makes it easier for developers to execute string formatting and concatenation in Python.
Moving on with this article on Python String Concatenation
Using the % operator
One of the most widely accepted methods of achieving string concatenation in Python is by using the % operator. To understand how it is exactly used in code, take a look at the example below.
x = ‘apples’ y = ‘lemons’ z = “In the basket are %s and %s” % (x,y)
In the above code, what the interpreter does is replace the values of %s with the values stored in variables x and y from the above lines. When the variable z is printed, the following result is obtained.
In the basket are apples and lemons
Moving on with this article on Python String Concatenation
Using the {} operator
While coding if you make use of the {}, it serves as a placeholder for the variables that you would like to store inside a string. But in order to pass variables inside a string, you first need to use the format() function.
One of the main advantages of using the format() function is that, you do not need to convert an integer into a string before concatenating the data; it will automatically do the same for you. This is one of the main reasons as to why this method of string concatenation is more preferred over others.
To understand this whole concept better, take a look at the example below.
Fname = “John” Lname = “Doe” Age = “24” print “{} {} is {} years old.“ format(fname, lname, age)
While running this code, the interpreter will take the appropriate values and store them as variables in the respective strings.
Moving on with this article on Python String Concatenation
Using the Join Method
Last but not the least is using the Join method in Python. The best use of this method is to concatenate a list of strings together in a single result. Take a look at the example below to understand this better.
>>> ‘ ‘ .join([‘the’, ‘quick’, ‘brown’, ‘fox’, ‘jumps’, ‘over’, ‘the’, ‘lazy’, ‘dog’])
‘the quick brown fox jumps over the lazy dog’
Now let us create a new list.
>>> music = [“Metallica”, “Rolling Stones”, “ACDC”, “Black Sabbath”, “Shinedown”]
Now to join both the strings we use the following.
>>> print ‘ ’.join(music) >>> print “ “.join(music)
This brings us to the end of this article on Python String Concatenation.
To get in-depth knowledge on Python along with its various applications, you can enroll here for live online training with 24/7 support and lifetime access.
Got a question for us? Mention them in the comments section of article and we will get back to you.