Python String Concatenation Tutorial

String concatenation is an important and popular topic for Python programming language. Python provides different functions and operators in order to concatenate strings in different ways. String concatenation is also called joining strings or gluing strings.

Python String Concatenation Ways

Two or more strings can be joined in Python by using different methods and operators. Here is the list of how to join two or more strings or string variables.

  • + Operator
  • Putting Strings Consecutively
  • * Operator
  • join() Method
  • format() Method

+ Operator

One of the most popular ways to join two or more strings is using the plus or + operator. The + operator also provides more readability than other ways. + operator also used for string values and string variables too. In the following example, we will define multiple strings and then concatenate them with the + operator.

a = "Python"

b = "Wisetut"

c = "Windows"

print( a + b )

print( a + b + c )

We can also use the + operator in order to concatenate two or more string values or literals like below.

a = "Python" + "Wisetut"
print(a)

b = "Python" + "Wisetut" + "Windows"
print(b)

print( "Python" + "Wisetut" )

print( "Python" + "Wisetut" + "Windows" )

Putting Strings Consecutively

Python also provides an interesting way in order to concatenate two or more strings without using any operator or method. Just putting two or more strings with single or more space delimiters will concatenate the given strings.

a = "Python" "Wisetut"
print(a)
#Output
#Pytho
nWisetut


b = "Python" "Wisetut"  "Windows"
print(b)
#Output
#Pytho
nWisetutWindows


print( "Python" "Wisetut" )
#Output
#Pytho
nWisetut


print( "Python" "Wisetut" "Windows" )
#Output
#Pytho
nWisetutWindows

* Operator

Even it is not a complete concatenation operator the * operator can be used to multiply a given string. In mathematical terminology * equal to the multiple concatenations like 3*2 means summing 3 for 2 times.

a = "Python" * 2
print(a)
#Output
#Pytho
nPytho
n


a = "Python" * 5
print(a)
#Output
#Pytho
nPytho
n
Pytho
nPytho
nPytho
n



print( "Python" * 2 )
#Output
#Pytho
nPytho
n


print( "Python" * 5
 )
#Output
#Pytho
nPytho
n
Pytho
nPytho
nPytho
n

join() Method

Python provides a powerful method named join() which is provided by a string data structure. join() method will join each element in the provided list. Before joining the element every element will be converted into a string.

names = [ "Python" , "Windows" , "Linux" ]

print("".join(names))
#Output
#Python,Windows,Linux

We can also provide some string or characters to put between list items during the join operation. For example, a comma can be used as a delimiter where every element in the list will be put together with a comma.

names = [ "Python" , "Windows" , "Linux" ]

print(",".join(names))
#Output
#Python,Windows,Linux

Alternatively, we can specify a word as a delimiter while joining the list items as strings. In the following example, we will use the string tect as the concatenation delimiter.

names = [ "Python" , "Windows" , "Linux" ]

print("tect ".join(names))
#Output
#Pythontect Windowstect Linuxtect

Up to now, we have called the join() method from a string value. We can also call the join() method via a string variable which value will be used as the delimiter string.

names = [ "Python" , "Windows" , "Linux" ]
a = "tect"

print(a.join(names))
#Output
#Pythontect Windowstect Linuxtect

format() Method

Python string variables and values or simply Python string type provides the format() method which is used to format a string. format() method provides advanced features where they can be used to concatenate multiple strings. Generally, the strings to be concatenated are provided as a parameter to the format() method like the following syntax.

"{} {} {}".format( STRING1 , STRING2 , STRING3 )
  • {} are used a locaters there provided STRING parameters will be put sequencilally
  • STRING parameters are used to provide string to concatanate. They can be 1 or more.

Lets make some example to learn the format() method to concatanete strings.

a = "Python"

b = "Windows"

c = "Linux"

x = "{} {} {}".format( a , b , c )
print(x)


y = "{} {} {}".format( a , "Windows" , "Linux" )
print(y)


z = "{} and {} and {}".format( a , b , "Linux" )
print(z)

We can also use numeric string descriptors in order to change their locations by providing the parameter index. The first parameter is expressed with {0} , second parameter with {1} , third parameter {3} etc.

a = "Python"

b = "Windows"

c = "Linux"

x = "{0} {1} {2}".format( a , b , c )
print(x)


y = "{2} {1} {0}".format( a , "Windows" , "Linux" )
print(y)


z = "{2} and {0} and {1}".format( a , b , "Linux" )
print(z)

Leave a Comment