DEV Community

Cover image for String Concatenation using Python
pavanbaddi
pavanbaddi

Posted on • Updated on • Originally published at thecodelearners.com

String Concatenation using Python

In Python, there are many ways of string concatenation.

Concatenation of string using join() method

join() method takes iterable which is list as parameter and outputted as a single string.

a = "welcome"
b = "to python"
c = "programming"

string1 = ' '

print(string1.join([a,b,c]))

# PYTHON OUTPUT
welcome to python programming

# or
l = ["welcome", "to python", "programming"]
string2 = ' ';
print(string2.join(l))

# PYTHON OUTPUT
welcome to python programming

Each item of iterable is concatenated with the existing item in the string which is shown below.

my_list = ["Python", "Django", "Django Queryset"]
string = '-->';
print(string.join(my_list))

# PYTHON OUTPUT
Python-->Django-->Django Queryset

Below are other ways of string concatenation with examples.

Strings can be repeated by this * asterisk operator

Example

x = 3*'hey-'
print(x)

#PYTHON OUTPUT
hey-hey-hey-

The string can be joined using this + addition operator

Example

x = 3*'hey-'+' new string joined'
print(x)

#PYTHON OUTPUT
hey-hey-hey- new string joined

Join Strings and Numbers

Example

x = 'John has scored '+72+' runs in '+4+' overs'
print(x)

#PYTHON ERROR OUTPUT
TypeError: Can't convert 'int' object to str implicitly


x = 'John has scored '+str(72)+' runs in '+str(4)+' overs'
print(x)

#PYTHON OUTPUT
John has scored 72 runs in 4 overs

runs = 70
overs = 4

x = 'John has scored '+str(runs)+' runs in '+str(overs)+' overs'

#PYTHON OUTPUT
John has scored 70 runs in 4 overs

Combine Multiple Strings

Example

string = ('This is string 1' 'This is string 2' 'This is string 3')

print(string)

#PYTHON OUTPUT
This is string 1 This is string 2  This is string 3

Concatenation using % Modulus operator

Example

string1 = 'Jake has won $' #type string
price = 100 #type int
string2 = ' price in swimming competition' #type string

print('%s%d%s'%(string1,price,string2))

#PYTHON OUTPUT
Jake has won $100 price in swimming competition

Concatenation using format() method

Example

string1 = 'Jake has won $' #type string
price = 100 #type int
string2 = ' price in swimming competition' #type string

print('{}{}{}'.format(string1,price,string2))

#PYTHON OUTPUT
Jake has won $100 price in swimming competition


#OR

string1 = 'Jake has won $' #type string
price = 100 #type int
string2 = ' price in swimming competition' #type string

print('{string1}{price}{string2}'.format(string1=string1,price=price,string2=string2))

#PYTHON OUTPUT
Jake has won $100 price in swimming competition

#OR

string1 = 'Jake has won $' #type string
price = 100 #type int
string2 = ' price in swimming competition' #type string

print('{string1}{price}{string2}'.format(string1='Jake has won $',price=100,string2=' price in swimming competition'))

#PYTHON OUTPUT
Jake has won $100 price in swimming competition

Python f string supported in version 3.6 and above

Example

string1 = 'Jake has won $' #type string
price = 100 #type int
string2 = ' price in swimming competition' #type string
print(f'{string1}{price}{string2}')

#PYTHON OUTPUT
Jake has won $100 price in swimming competition

This article was originally posted on website: the-code-learners python string concatenation

Top comments (1)

Collapse
 
pavanbaddi profile image
pavanbaddi

Thank you for reminding. I have updated the post.