Python String Concatenation

There are multiple ways to concatenate string in Python. You can use the traditional + operator, or String 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.

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

Python String concatenation using a arithmetic operator + output

ab

If you use the * operator along with a numeric value, then that string repeated by a given number. 

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

String concatenation or repeat output

HiHi
HiHiHiHi

Instead of directly performing Python string concatenation, we assigned the concat value to str3

str1 = 'Hello'
str2 = 'World'
 
str3 = str1 + str2
print(str3)
HelloWorld

While doing the string concatenation, you should be careful with the separator between two strings. Python doesn’t provide any separator or delimiter so, you have to manually concat that extra spaces.

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

Tutorial Gateway

Tutorial, Gateway

It is another example of a string concatenation. Here, we used += Operator.

str1 = 'Tutorial '
str2 = 'Gateway'
 
str1 += str2
print(str1)

String concatenation using += Operator

Tutorial Gateway

Python String Concatenation example 2

By placing the strings with the print statement separated by a comma concat 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

String Concatenation example 3

Python has the string format function. You can use this format function 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('\nAfter String Concatenate = ', str_concat1)
print()
 
str_concat2 = '{} {}'.format(str1, str2)
print('After String Concatenate = ', str_concat2)
print()
 
str_concat3 = '{} - {}'.format(str1, str2)
print('After String Concatenate = ', str_concat3)
print()
 
str_concat4 = '{a} **+** {b}'.format(a = str1, b = str2)
print('After String Concatenate = ', str_concat4)

String Concatenation using format function output


After String Concatenate =  TutorialGateway

After String Concatenate =  Tutorial Gateway

After String Concatenate =  Tutorial - Gateway

After String Concatenate =  Tutorial **+** Gateway

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

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

String Concatenation using %s format output


After String Concatenate =  TutorialGateway

After String Concatenate =  Python Tutorial Gateway

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

After String Concatenate =  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)

String and int Concatenation output

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.

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

Python String Concatenation example 4

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 strings to perform string concatenation.

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

After String Concatenate =  PythonTutorial

After String Concatenate =  Python Tutorial

After String Concatenate = Python Tutorial

After String Concatenate = Python Tutorial

After String Concatenate =  Python Tutorial

Python String Concatenation using Join

In Python, we have String Join function to join two or more strings. You can use this function to concatenate 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('\nAfter String Concatenate = ', str_concat1)
print()
 
str_concat2 =  ', '.join(str_list)
print('After String Concatenate = ', str_concat2)
print()
 
str_concat2 =  ' $ '.join(str_list)
print('After String Concatenate = ', str_concat2)

String Concatenation using Join output


After String Concatenate =  Apple Orange Mango Kiwi Cherry

After String Concatenate =  Apple, Orange, Mango, Kiwi, Cherry

After String Concatenate =  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 strings to an actual list before joining them. 

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

String concatenation using join function output.


After String Concatenate =  Hello  World 

After String Concatenate =  Hello , World , Tutorial 

After String Concatenate =  Hello  *+* World  *+* Tutorial  *+* Gateway