Python String Concatenation

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

Python string concatenation Example

It is the basic example of Python string concatenation. Here, we are using Python + operator to concatenate a and b. If you use the * operator along with a numeric value, then that sentence repeated by a given number.

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

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

Python String concat using a arithmetic operator + output

ab
HiHi
HiHiHiHi

While doing the Python string concatenation, you should be careful with the separator between two. Python doesn’t provide any separator or delimiter so, you have to manually concat that extra spaces. In the last statement, we used += Operator which 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 example 2

By placing the words with the print statement separated by a comma will merge strings — for instance, the first print statement concat str1 and str2.

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

Python String Concatenation example 3

Python has the format function. You can use this format function curly braces to concat strings. Here, {} represent the string. You can use any separator like spaces or comma in-between those {} {}.

str1 = 'Tutorial'
str2 = 'Gateway'
 
str_concat1 = '{}{}'.format(str1, str2)
print(str_concat1)
print()
 
str_concat2 = '{} {}'.format(str1, str2)
print(str_concat2)
print()
 
str_concat3 = '{} - {}'.format(str1, str2)
print(str_concat3)
print()
 
str_concat4 = '{a} **+** {b}'.format(a = str1, b = str2)
print(str_concat4)

TutorialGateway

Tutorial Gateway

Tutorial - Gateway

Tutorial **+** Gateway

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

str1 = 'Tutorial'
str2 = 'Gateway'
str3 = 'Python'
year = 2018
 
str_concat1 = '%s%s' %(str1, str2)
print('\nAfter String Concat = ', str_concat1)
print()
 
str_concat2 = '%s %s %s' %(str3, str1, str2)
print('After String Concat = ', str_concat2)
print()
 
str_concat3 = 'Learn %s at %s %s - Year %d' %(str3, str1, str2, year)
print('After String Concat = ', str_concat3)
print()
 
str_concat4 = '%s %s %s %s' %(str1, str2, str3, year)
print('After String Concat = ', str_concat4)

Concatenation using %s format output


After String Concat =  TutorialGateway

After String Concat =  Python Tutorial Gateway

After String Concat =  Learn Python at Tutorial Gateway - Year 2018

After String Concat =  Tutorial Gateway Python 2018

Python String and int Concatenation

Unlike most of the programming languages, Python doesn’t implicitly convert an integer to string to perform concat. As you can see, it was throwing an error TypeError: can only concatenate 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 concatenate str (not "int") to str

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

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

Let me use all the available options to concatenate strings in one program.

text = 'Tutorial Gateway '
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}')
Tutorial Gateway 2018

Tutorial Gateway 2018

Tutorial Gateway 2018

Tutorial Gateway  2018

Tutorial Gateway 2018

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

str1 = input('Please enter the First : ')
str2 = input('Please enter the Second : ')
 
str_concat1 = str1 + str2
print(str_concat1)
print()
 
str_concat2 = str1 + ' ' + str2
print(str_concat2)
print()
 
print('{} {}'.format(str1, str2))
print()
 
print('%s %s' %(str1, str2))
print()
 
print(str1, str2)
Please enter the First : Python
Please enter the Second : Tutorial

PythonTutorial

Python Tutorial

Python Tutorial

Python Tutorial

Python Tutorial

In Python, we have Join function to join two or more strings. You can use this function to concatenate list of strings. ‘ ‘.join(str_list) concat strings in str_list separated by space.

str_list = ['Apple', 'Orange', 'Mango', 'Kiwi', 'Cherry']
 
str_concat1 = ' '.join(str_list)
print(str_concat1)
print()
 
str_concat2 =  ', '.join(str_list)
print(str_concat2)
print()
 
str_concat2 =  ' $ '.join(str_list)
print(str_concat2)

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 strings.

str1 = 'Hello '
str2 = 'World '
str3 = 'Tutorial '
str4 = 'Gateway'
 
str_concat1 = ' '.join([str1, str2])
print(str_concat1)
print()
 
str_concat2 = ', '.join([str1, str2, str3])
print(str_concat2)
print()
 
str_concat3 = ' *+* '.join([str1, str2, str3, str4])
print(str_concat3)

Hello  World 

Hello , World , Tutorial 

Hello  *+* World  *+* Tutorial  *+* Gateway