Concatenation (+) and repetition (*)

Next, comes concatenation and repetition. Refer to the following code:

>>> 3 * 'hi' + 'hello'
'hihihihello'

In the preceding example, we are doing string concatenation and repetition. 3 * 'hi' means hi gets printed 3 times and, using the + sign, we are joining the hello string next to hi.

We can automatically concatenate two strings just by writing them next to each other. These two strings must be enclosed between quotes, as shown here:

>>> 'he' 'llo'
'hello'

This feature is really helpful when you have long strings and you want to break them. Here is an example:

>>> str = ('Several strings'
... 'joining them together.')
>>> str
'Several strings joining them together.'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset