What is the difference between single, double, and triple quotes in Python? [duplicate]

半腔热情 提交于 2019-12-30 09:36:08

问题


In other words, how do I know which one to use?

I know when I use strings. I would do

    string = "This is a string"

When would I use ' ' or """ """?


回答1:


'...' and "..." are equivalent. If you have an apostrophe in the string, it is easier to use "..." so you don't have to escape the apostrophe. If you have quotes in the string, it's easier to use '...' so you don't have to escape the quotes.

Triple quotes (both varieties, """ and ''' are permitted) allow the string to contain line breaks. These are commonly used for docstrings (and other multi-line comments, including "commenting out" code) and for embedded snippets of other computer languages such as HTML and SQL.

https://docs.python.org/2.0/ref/strings.html



来源:https://stackoverflow.com/questions/35659017/what-is-the-difference-between-single-double-and-triple-quotes-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!