June 2017
Beginner
352 pages
8h 39m
English
If you want a literal string containing newlines, you have two options:
First, let's look at multiline strings. Multiline strings are delimited by three quote characters rather than one. Here's an example using three double-quotes:
>>> """This is... a multiline... string"""'This is\na multiline\nstring'
Notice how, when the string is echoed back to us, the newlines are represented by the \n escape sequence.
We can also use three single-quotes:
>>> '''So... is... this.''''So\nis\nthis.'
As an alternative to using multiline quoting, we can just embed the control characters ourselves:
>>> m = 'This string\nspans mutiple\nlines'>>> m'This string\nspans mutiple\nlines' ...
Read now
Unlock full access