September 2013
Intermediate to advanced
350 pages
9h 38m
English
If you create a string using single or double quotes, the whole string must fit onto a single line.
Here’s what happens when you try to stretch a string across multiple lines:
| | >>> 'one |
| | File "<stdin>", line 1 |
| | 'one |
| | ^ |
| | SyntaxError: EOL while scanning string literal |
As we saw in Creating Strings of Characters, EOL stands for “end of line”; so in this error report, Python is saying that it reached the end of the line before it found the end of the string.
To span multiple lines, put three single quotes or three double quotes around the string instead of one of each. The string can then span as many lines as you want:
| | >>> '''one |
| | ... two |
| | ... three''' |
| | 'one\ntwo\nthree' |
Read now
Unlock full access