String Concatenation in Python
It is an operation of clipping one string to the end of another string. You can use the concatenation operation by using the + sign. % allows you to insert a string into another specified string value.
Both the operators are used to concatenate strings in python.
When you are operating with a string in python you may need to combine it with another string.
Python provides a number of string methods that can perform string concatenation.
This article will help you out how to use + and %operators to concatenate strings in python.
How are you able to concatenate strings in python?
● String concatenation by using + operator- The + operator allows you to combine two or more strings.
The + operator must be in between the two strings which are to be concatenated.
SYNTAX : print(“string1” + “string2”)
You can use the syntax to merge labels with the employer’s contact information to get your desired results.
some of the examples that merges the data :
print(“Name:” + “Shivam”) print (“Department:” + “Clerk”)
As you have noticed, all two strings on each line have been concatenated with the operator +. Both the lines have been executed with the spaces given in the time of input.
You can only add or clip string data types.
One integer value and one string cannot be added together.
Let’s make it clear through an example
print(“john” + 22)
Type error: can concatenate str(not int) to str:
We can resolve this error by using str ()for the conversion of integers to a string. Have a look at this solution
print(“john” + str(22))
Here are some examples for you to get a clear understanding of concatenation.
1. Clipping variable a with variable b into variable c:
x = "Hello" y = "World" z = x + y print(z)
2. To add a gap between the variables , add a ” “:
a = "Hello" b = "World" c = a + " " + b print(c)
Concatenation of strings by percentage operator:
The % string concept merges a string into another string. This action is commonly known as Python string interpolation.
You can add on insert the string between two strings in a specified place in the string.
print("%s %s" % ("Hey", "Studyber"))
To make it more clear, here are some examples. Have a look at that.
Example no 1.
s1 = 'Hey' s2 = 'Studyber' s3 = "%s %s" % (s1, s2) print('String Concatenation using % Operator =', s3) s3 = "%s %s from Shivi - %d" % (s1, s2, 2021) print('String Concatenation using % Operator with Accessing =', s3)
String Concatenation using % Operator = Hey Studyber
String Concatenation using % Operator with Accessing = Hey Shivam from Shivi – 2021
str1 = "Hello" str2 = "Studyber" print("% s % s" % (str1, str2))
In both the above cases % is used to concatenate the strings, pass the value and return the final results to the variable.
These were some of the basics ideas on the concatenation of strings in python. I hope you all enjoyed going through it with the practical examples. If any doubt or queries are left please comments in the comment section we” ll get to you ASAP.