Creating a Multiline String

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. The string can then span as many lines as you want:

 >>>​​ ​​''​​'​​one
 ...​​ ​​two
 ...​​ ​​three​​''​​'
 'one two three'

Notice that the string Python creates contains a sequence everywhere our input started a new line. Each newline is a character in the string.

..................Content has been hidden....................

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