Книга: Practical Programming, Fourth Edition
Назад: Using Special Characters in Strings
Дальше: Printing Information

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 "<python-input-3>", line 1
  'one
  ^
 SyntaxError: unterminated string literal (detected at line 1)

In this error report, Python indicates that it reached the end of the line before it encountered the end of the string.

Use triple quotes for multiline strings

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\ntwo\nthree'

Newline \n and tab \t are escape sequences

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

Назад: Using Special Characters in Strings
Дальше: Printing Information