Concatenation of Multiple Strings in Python

A concatenation is the joining of two or more strings into one sitting. In Python, we use the + operators to achieve this. When you write two string literals together, it also concatenates them. We use the * operator to repeat the strings a particular number of times.

str1 = ‘Hello’

str2 = ‘World’

# using + print (str1 + str2 = ‘, str1 + str2)

# using * print (str1 * 3 = ‘, str1 * 3)

When you write two string literals, this also concatenates them just like the + operator. You can also use parentheses if you want to proceed to concatenate strings in separate lines.

>>> # two string literals together

>>> ‘Hello “World!’

‘Hello World!’

>>> # using parentheses

>>> s = (‘Hello’…’World’)

>>> s ‘Hello World’

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s