Python String Concatenation

There are multiple ways to concatenate strings. For Concatenation, you can use the traditional + operator or Python Join or separate the string text using a comma. In this section, we discuss how to do string concatenation with examples.

Python string concatenation using the + operator

It is the basic example of Python string concatenation and we are using the + operator to concatenate two a and b.

If you use the * operator along with a numeric value on a single string object, then that sentence is repeated by a given number and returns multiple words.

str1 = 'a' + 'b'
print(str1)

str2 = 'Hi' * 2
print(str2)
 
str3 = 'Hi' * 4
print(str3)

concatenate strings using an arithmetic operator + and return a new output

ab
HiHi
HiHiHiHi

While doing the concatenation, you should be careful with the separator between two string variables.

Python doesn’t provide any separator or delimiter. So, you have to manually add that extra space when adding multiple strings. In the last statement, we used += Operator, who will add str2 to str1 and updates the original.

str1 = 'Tutorial'
str2 = 'Gateway'
 
str3 = str1 + str2
print(str3)
print()
 
str4 = str1 + ' ' + str2
print(str4)
print()
 
str5 = str1 + ', ' + str2
print(str5)

str1 += str2
print(str1)
TutorialGateway

Tutorial Gateway

Tutorial, Gateway

Tutorial Gateway

Python String Concatenation using a comma

Placing the words with the print statement separated by a comma will merge sentences. For instance, the first print statement in this example concat str1 and str2.

str1 = 'Hello '
str2 = 'World '
str3 = 'Tutorial '
str4 = 'Gateway'
 
print('\nAfter = ', str1, str2)
print()
 
print('After = ', str1, str2, str3)
print()
 
print('After = ', str1, str2, str3, str4)
Python string Concatenation 6

Python String Concatenation using a format

It has a format function. You can use this format function with curly braces for this. Here, {} represent the sentence or word. You can use any separator like spaces or commas in-between those {} {}.

str1 = 'Tutorial'
str2 = 'Gateway'
 
c1 = '{}{}'.format(str1, str2)
print(c1)
print()
 
c2 = '{} {}'.format(str1, str2)
print(c2)
print()
 
c3 = '{} - {}'.format(str1, str2)
print(c3)
print()
 
c4 = '{a} **+** {b}'.format(a = str1, b = str2)
print(c4)

TutorialGateway

Tutorial Gateway

Tutorial - Gateway

Tutorial **+** Gateway

In Python, you can use the %s format option to concat strings, so we use this % symbol to concatenate two or more strs.

str1 = 'Tutorial'
str2 = 'Gateway'
str3 = 'Python'
year = 2018
 
c1 = '%s%s' %(str1, str2)
print(c1)
print()
 
c2 = '%s %s %s' %(str3, str1, str2)
print(c2)
print()
 
c3 = 'Learn %s at %s %s - Year %d' %(str3, str1, str2, year)
print(c3)
print()
 
c4 = '%s %s %s %s' %(str1, str2, str3, year)
print(c4)

Combine using %s format output


TutorialGateway

Python Tutorial Gateway

Learn Python at Tutorial Gateway - Year 2018

Tutorial Gateway Python 2018

Python String and int Concatenation using str

Unlike most programming languages, Python doesn’t implicitly convert an integer to a string to perform concatenation. As you can see, it was throwing an error TypeError: can only on str (not “int”) to str

text = 'Tutorial Gateway'
year = 2018
 
print(text + year)
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 4, in <module>
    print(text + year)
TypeError: can only on str (not "int") to str

To resolve this, you have to use the str function to convert that int value year to str literal.

text = 'Tutorial Gateway '
year = 2018
 
print(text + str(year))
Tutorial Gateway 2018

Let me use all the available options in one program.

text = 'Hello World '
year = 2018
 
print(text + str(year))
print()
 
print('{}{}'.format(text, year))
print()
 
print('%s%s' %(text, year))
print()
 
print(text, year)
print()
 
print(f'{text}{year}')
Hello World 2018

Hello World 2018

Hello World 2018

Hello World  2018

Hello World 2018

This example is the same as the above. However, this time we are allowing the user to enter their own sentences to perform this.

str1 = input('Please enter the First : ')
str2 = input('Please enter the Second : ')
 
a = str1 + str2
print(a)
print()
 
b = str1 + ' ' + str2
print(b)
print()
 
print('{} {}'.format(str1, str2))
print()
 
print('%s %s' %(str1, str2))
print()
 
print(str1, str2)
Please enter the First : Java
Please enter the Second : Tutorial

JavaTutorial

Java Tutorial

Java Tutorial

Java Tutorial

Java Tutorial

Python String Concatenation using join

We have the Join function to join two or more sentences. You can use this function to combine a list of words. ‘ ‘.join(str_list) combine all the words in str_list separated by space.

str_list = ['Apple', 'Orange', 'Mango', 'Kiwi', 'Cherry']
 
a = ' '.join(str_list)
print(a)
print()
 
b =  ', '.join(str_list)
print(b)
print()
 
c =  ' $ '.join(str_list)
print(c)

Apple Orange Mango Kiwi Cherry

Apple, Orange, Mango, Kiwi, Cherry

Apple $ Orange $ Mango $ Kiwi $ Cherry

You don’t have to use the list to use this Join function. You can convert a list of different words to an actual list before joining.

s1 = 'Hello '
s2 = 'World '
s3 = 'Welcome '
s4 = 'Guys'
 
co1 = ' '.join([s1, s2])
print(co1)
print()
 
co2 = ', '.join([s1, s2, s3])
print(co2)
print()
 
co3 = ' *+* '.join([s1, s2, s3, s4])
print(co3)

Hello  World 

Hello , World , Welcome 

Hello  *+* World  *+* Welcome  *+* Guys