Search Here

Python String Concatenation | Join(), Concatenation using Modulus Operator, format(), f-Strings etc

Python String Concatenation

In this tutorial of Python String Concatenation, Python strings can be appended or joined through a process called concatenation were various methods some are as easy as using placeholders like {} braces and for more control functions comes to rescue.

Post Update Timeline

Updated on: 13-07-2020

Added new topics such as Chaining strings, Concatenation using modulus operator and use of “f” strings in Python

Chaining of string using join() method

The join() the method takes a list as a parameter and returns a single concatenated 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

In the above example, each item of the list is appended with the existing item in the string and also explained using another example below.

Another example using the join() method for concatenation strings

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

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

Other ways of string concatenation with examples.

Note

Strings can be repeated by this `*` asterisk operator

Example: Using asterisk operator to multiple strings

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

#PYTHON OUTPUT
hey-hey-hey-

Using this addition operator for string concatenation

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

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

Concatenating Numbers and Strings together

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

Note

Although using addition operators is an old way of concatenation you can still use it. But it variable is numeric than wrap it inside the str() function.

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 the Modulus operator

Although, there are other methods this one provides a simple approach as a result you may also find it easier.

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

Using format() method

For instance, if your looking for other ways than format() function helps you in easily putting strings together as shown in below snippet.

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

Above all, snippets are those various approaches of using format function.

Using f strings

This feature was introduced in Python Version 3.6. Therefore, python developers recognized its capability of quick string formation.

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

Conclusion

In conclusion, of this post on Python String Concatenation, you now have learned to use them in a much better way and Therefore we have come to the end of this post. In addition, for doubts regarding programming, you can comment or contact us you’ll have our full support.

Recent Posts

Summary
Review Date
Reviewed Item
Python String Concatenation
Author Rating
51star1star1star1star1star
Software Name
Python Programming Language
Software Name
Windows Os, Mac Os, Ubuntu Os
Software Category
Programming Language