Concatenation of Strings in Python

20-12-22 Ahmed Obaid 2412 0

​We call the merging of strings together concatenation or concatenation. Python offers a number of methods that can be used to combine separate strings and return a new string.

In Python, you can join two different strings together or the same string with itself multiple times using operator ( + ), operator ( * ), operator ( %) as well as the join() and format() functions.

Use operator ( + ) to combine text strings

More than two strings can be joined using the operator ( + ) as in the following example:


a1 = "Learn"
a2 = "Python"
a3 = a1 + a2
a4 = a1 + " " + a2
print(a3)
print(a4)

The output will be:


LearnPython
Learn Python

Note that the first sentence appeared without a white space between the two words because the operator (+) was placed directly between the two variables. As for the second sentence, the white space appeared because we put two quotation marks between them and an empty space, and we combined them with the sentence

How to use operator ( * ) To combine and duplicate strings 

The same string can be appended to another string using the operator (*) and the way it is formulated like this str=str * x . where x is the number of times the string repeats:

Example:


string = "Hello"
print("String 1:", string)
string = string * 3
print("Concatenated same string:", string)

 The output will be:


String 1: Hello
Concatenated same string: HelloHelloHello

 Use the operator ( % ) to combine strings

 The operator ( %) merges a string into another string. This process is called Python's string interpolation. You can add a string to any position in an existing string using the operator ( %) . Note that the operator ( + ) adds a value to the end of the string, but the operator ( %) can add a value to the position you specify in the string. The operator ( %) also allows you to insert a string into another string

 We can use operator ( %) to combine two strings in Python. As in the following example.


a1 = "Learn"
a2 = "Python"
a3 = "%s %s" % (a1, a2)
a4 = "%s%s" % (a1, a2)
print(a3)
print(a4)

 The output will be:


Learn Python
LearnPython

 Notice the white space between s% in the variable a3 .

 Let's say we have two names that we want to appear in a string. This is the code we can use to add these names to our string:


names = "%s and %s learn Python." % ("Ahmed", "Hamza")
print(names)

 The output will be:


Ahmed and Hamza learn Python.

 We use s% to say that we want a specific value to appear at that position. We use the % operator to indicate where we want the value to appear in our string.

 Use the join() function to join strings

 The join() function can be used to join a series of elements. It can also be used to combine two strings.

 Example:


a = "Love"
b = "Python"
c = "".join([a,b])
h = " ".join([a,b])
print(c)
print(h)

 The output will be:


LovePython
Love Python

 Notice the white space between the quotation marks in the variable h .

 We will explain the join() function in detail in a special article later

 Use the format() function to merge strings

 The format() function can be used to format a string. It can also be used to combine two strings.

 Example:


a = "Love"
b = "Python"
c = "{}{}".format(a,b)
h = "{} {}".format(a,b)
print(c)
print(h)

 The output will be:


LovePython
Love Python

 {} specifies the position of string variables.

 Notice also the white space between the brackets '{} {}' in the variable h . Because it is responsible for the space between the two words

 We will explain the format() function in detail in a special article undefined Later

 External sources for more information:

 concatenation - The official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python python data types Python String Concatenation Strings

Share page