How to build up a large string out of smaller strings in Python

Let’s say you may want to construct a big string from smaller strings in Python. The solution is simple and it is called concatenation.
As in Math, you can use the addition operator, which is +, to put one or more strings together and return a larger string.

>>> s1 = 'My name '
>>> s2 = 'is Dan.'
>>> new_string = s1 + s2
>>> print(new_string)
My name is Dan.

Or, if you don’t want to declare the small strings as variables you could just do as shown below:

>>> new_string = 'My name ' + 'is Dan.'
>>> print(new_string)
My name is Dan.

Strange enough, you can multiply strings using the * operator.

>>> s1 = 'My name '
>>> s2 = 'is '
>>> s3 = 'Dan'
>>> s4 = ' Dumitrache.'
>>> new_string = s1 + s2 + s3 * 3 + s4
>>> print(new_string)
My name is DanDanDan Dumitrache.

Note:
Division and subtraction don’t work with strings.

Leave a Reply