fb pixel

Multi-line strings

So far, we have declared strings with single or double quotation marks ( or "). That prevents us from declaring strings that span multiple lines as python expects to see a closing quotation mark at the end of the line. Declaring multi-line strings is possible through triple quotation marks:
text = '''This long long text 
can be split
into several lines.
Like this!'''
text = """This long long text 
can be split
into several lines.
Like this!"""
We can have multi-line strings with both double " quotes and single ' quotes. Both of these programs will print the same output:
This long long text 
can be split
into several lines.
Like this!
Multi-line strings are especially useful for long messages, texts, or documentation. Developers who want to describe their code and what it does can do it with comments for a single-line small remark, but when describing the behavior of a chunk of code, it’s more usual to see programmers use multi-line strings. We will discuss wiring documentation and some best practices later in the course.


To be, or not to be, that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take arms against a sea of troubles
And by opposing end them. To die—to sleep,
No more; and by a sleep to say we end
The heart-ache and the thousand natural shocks
That flesh is heir to: 'tis a consummation
Devoutly to be wish'd. To die, to sleep;
To sleep, perchance to dream—ay, there's the rub:
For in that sleep of death what dreams may come,
When we have shuffled off this mortal coil,
Must give us pause—there's the respect
That makes calamity of so long life.
The texts of William Shakespeare are very famous and very lengthy at the same time. It would be very difficult to read them on the same line. That’s why you are asked to keep the text in a single multi-line variable. The task is to print the text in the output, followed by its length:
Another question: Can you count the actual number of symbols in the text including spaces? How many are they? Is the program printing a different number?
The program prints a bigger number compared to the number of characters in the text. This is due to the fact that each new-line character is considered a separate character by Python. There are 589 characters, but as python counts each new line as a new character, we get 13 more characters. Therefore, the program prints 602.
To check your solution you need to sign in
Sign in to continue