Multi-line strings with Python

When we are working with Python and we handle very large text strings it will be convenient to know how to handle multi-line strings with Python.

It all starts when we have a very long text string to manage:

In a place in La Mancha, the name of which I do not want to remember, not long ago there lived one of those gentlemen with a lance in a shipyard, an old buckler, a skinny nag and a racing greyhound.

And we must encode it within our source code. The first option can be directly:

text = "In a place in La Mancha, the name of which I do not want to remember, not long ago there lived one of those gentlemen with a lance in a shipyard, an old buckler, a skinny nag and a racing greyhound."

Although it will be unwieldy within our editor and will have poor readability.

This is why Python allows us to have multi-line strings. To do this we will simply have to use the «» symbol to separate each of the lines:

text = "line 1"
       "line 2"
       "line 3"
       ...
       "last line"

Thus our phrase within a program in Python will look like this:

text = "In a place in La Mancha, whose name I don't want to remember, "
        "not long ago there lived a nobleman of those who keep a lance in the shipyard,"
        "ancient buckler, skinny nag and racing greyhound."

I’m sure that from now on you will use multi-line strings more with Python to improve the readability of your programs.