- The Python Apprentice
- Robert Smallshire Austin Bingham
- 136字
- 2025-04-04 19:04:57
String quoting styles
Literal strings in Python are delimited by quotes:
>>> 'This is a string'
You can use single quotation marks, as we have above. Or you can use double quotation marks, as shown below:
>>> "This is also a string"
You must, however, be consistent. For example, you can't use a double quotation mark paired with a single quotation mark:
>>> "inconsistent'
File "<stdin>", line 1
"inconsistent'
^
SyntaxError: EOL while scanning string literal
Supporting both quoting styles allows you to easily incorporate the other quote character into the literal string without resorting to ugly escape character gymnastics:
>>> "It's a good thing."
"It's a good thing."
>>> '"Yes!", he said, "I agree!"'
'"Yes!", he said, "I agree!"'
Notice that the REPL exploits the same quoting flexibility when echoing the strings back to us.