Tip 4 Use Quotes of All Sorts

Pythonic Programming — by Dmitry Zinoviev (12 / 116)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Know Ownership and Licensing | TOC | Keep Letter Case Consistent 👉

★2.7, 3.4+ You can enclose string literals in Python in four types of quotation marks. Single and double quotation marks define single-line strings. Line breaks are not allowed within:

​ ​'Mary had a little lamb'​
​ ​"Mary had a little lamb"​

There is no difference whatsoever between single and double quotation marks in Python, unlike C/C++/Java (where single marks are used for individual characters and double marks are used for strings) or PHP (where double-quoted strings are interpreted but single-quoted strings are not). Should you use single or double quotation marks? It is up to you.

Triple-single and triple-double quotation marks define multiline strings. They can include literal line breaks.

​ ​'''Mary​
​ ​had a little​
​ ​lamb'''​

​ ​"""Mary​
​ ​had a little​
​ ​lamb"""​

And again, there is no difference whatsoever between triple-single and triple-double quotation marks. Use whatever you like. Just try to be consistent.

👈 Know Ownership and Licensing | TOC | Keep Letter Case Consistent 👉

Pythonic Programming by Dmitry Zinoviev can be purchased in other book formats directly from the Pragmatic Programmers. If you notice a code error or formatting mistake, please let us know here so that we can fix it.

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.