Concatenate Strings in Python

In this article, we would see how to concatenate two strings in Python. The concept mainly is joining two or more strings in one string in Python.

Understand it with the help of couple of examples. Let’s say we have –

string, s1 = “Hello “

and, string s2 = “World!”

Now, when we join or concatenate these two strings. The subsequent outcome has to be ‘Hello World!’

We can achieve this through following code (Note: We have put a space after Hello in s1 variable, this is just for legibility)-

>>> s1 = "Hello "
>>> s2 = "World!"
>>> x=s1+s2
>>> print(x)

The outcome of the above code would be –

'Hello World!'

Similar results could also be achieved if we directly join the strings without using variables –

>>> "Hello " + "World!"

If we don’t want add space in “Hello ”┬ástring, then it can be done through –

>>> s1 = "Hello"
>>> s2 = "World!"
>>> x=s1+" "+s2
>>> print(x)

Notice, the difference from above code. We have added a ” ” to the variable x. Earlier, to achieve similar results we had to add a space at the end of string s1.

We use + operator to concatenate the strings. Otherwise, it would work as a mathematical operator on numbers. Also, its not possible to add a string and integer (int) directly through + operator. We need to bring in certain modifications. We reserve that discussion in the next article we publish.

In conclusion, we have discussed how to Concatenate Strings in Python.