Monday, March 16, 2020

String Concatenation in Python Language



As we all now the Python language is one of the most powerful languages of the present time. As in the previous languages, we have char data type as the primitive data type but this is something different in the Python language. This means that Python does not have a data type corresponding to the character. In Python language, strings have type str. String in Python language represents the sequence of characters. In Python language, the strings are immutable which means that once created they cannot be changed or modified. This means when we want to modify the string it can not be done in the existing string a new string required to be created.
The word concatenation means to join some objects together. In Python language, the following methods are used to join two or more string together:-
  1. + operator
  2. Using join() method
  3. % operator
  4. Using format() function
  5. Using f-string (Interpolation of literal string)
Concatenation using + operator
In Python Language, the + operator is used to merge two or more string. for example, suppose we have two strings as follows
a="This is "
b="Power of Python"
c=a+b
print(c)
Considering the above program we have a string store in which is "This is " and b store "Power of Python" then we have concatenated the two string into the third-string called c. So when we print the value of variable c then it will print "This is Power of Python". Similarly, we can add more than two string together. for example
a="First "
b="Second "
c="Third "
d="Fourth"
e=a+b+c+d
print(e)
The output string will be "First Second Third Fourth".
The point here to be noted that we can not append the value of the same string. for example, suppose we have the following code:
a="First"
b=7
c=a+b
print(c)
This code will generate the following error
File "C:/Users/DELL/.spyder-py3/temp.py", line 9, in <module>
    c=a+b
TypeError: can only concatenate str (not "int") to str
from this error, we came to know + operator only works when both values are str only.
Concatenation using join() method
The string concatenation, when done by + operator, doesn't provide any option to print any delimiter. So if we want to print the string with separator we can do it using join() method. for example
s1="This"
s2="Python"
s3="Language"
s4=" ".join([s1,s2,s3])
print(s4)
The above course will give the following output
This Python Language
if we do not want any delimiter then we can use empty string before the join() method. for example
s1="This"
s2="Python"
s3="Language"
s4="".join([s1,s2,s3])
print(s4)
The output of this code is
ThisPythonLanguage
We can also put any character to separate each string with others. for example
s1="This"
s2="Python"
s3="Language"
s4=", ".join([s1,s2,s3])
print(s4)
Output
The above code will give output as This, Python, Language
Concatenation using % operator
Another way by which we can perform the concatenation of two or more strings is by using the % operator. Like in C language the % sign is called the format specifier. So we can use %s to determine the string, %d can use to specify integer, etc. Consider the following code:
s1="This"
s2="Python"
s3="Language"
s4="%s %s %s " % (s1,s2,s3)
print(s4)
The output of this code will be 
This Python Language
To analyze the above code there are three strings s1,s2, and s3. These strings are represented by individual %s symbol and by % operator get concatenated into the fourth string s4. Now take another example:
s1="The"
s2="sum"
s3="of"
a=int(input("Enter the first Number"))
b=int(input("Enter the Second Number"))
c=a+b
s4="%s %s %s %d and %d is %d " % (s1,s2,s3,a,b,c)
print(s4)
The output of this code will yield the following:
Enter the first Number10
Enter the Second Number70
The sum of 10 and 70 is 80
Concatenation of strings using the format() method
The format() function used with strings is a very a versatile and powerful function used for formatting strings. Using this method for concatenation is the utilization of just one feature of format() method. The format strings method has curly braces { } as the placeholders or replacement arguments or fields which get replaced. Here we can use positional arguments or keyword arguments to specify the order of the fields that have to be replaced. Consider the following code
s1="{},{} and {}".format('One','Two','Three')
print("Default sequence of argument is :",s1)
s2="{1},{0} and {2}".format('One','Two','Three')
print("Positional Sequence of argument is : ",s2)
The output of this code will be
Default sequence of argument is: One, Two and Three
Positional Sequence of argument is: Two, One and Three
So we can see that in string s1 we store the list of three arguments "One, Two and Three" which become the default sequence and in the second string s2 we have passed the positional sequence.
It is very unfair to use the format() method only for the string concatenation but it is one of the methods which can be used for this purpose also.
Using f-string (Interpolation of literal string)
The f-string methods can only be used if we are using Python 3.6 onwards. Consider the following code where the f-string method is used for the concatenation purpose.
s1="Python"
s2="Language"
s3="is"
s4="Powerful"
s5=f'{s1} {s2} {s3} {s4}'
print(s5)
The output of the above code will be as follows
Python Language is Powerful
The value of s1, s2, s3, and s4 gets stored in s5.
So to conclude we can use string concatenation in python in several ways. We have to use different methods depending upon the requirement or our needs in the program. Suppose if we want to concatenate the strings using delimiter we can use join() method. If we know the usage of % we can use it for the concatenation method. We can use format() method also but the format() method can be used for more things than simply for concatenation. If we are using the updated version of python we can use the f-string method which more decent to use when compared to format() method. In my opinion for simple concatenation of strings, we should use the + operator which is very easy among all other options available.




No comments:

Post a Comment