Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 """ """?

like image 889
katherinethegrape Avatar asked Feb 26 '16 18:02

katherinethegrape


People also ask

What is the difference between single and double quotes in Python?

Generally, double quotes are used for string representation and single quotes are used for regular expressions, dict keys or SQL. Hence both single quote and double quotes depict string in python but it's sometimes our need to use one type over the other.

What does 3 quotation marks mean in Python?

Note: Triple quotes, according to official Python documentation are docstrings, or multi-line docstrings and are not considered comments. Anything inside triple quotes is read by the interpreter. When the interpreter encounters the hash symbol, it ignores everything after that. That is what a comment is defined to be.

What is the difference between single double and triple quotes in Python Mcq?

Using single and double quotes are equivalent. The only difference is when we use an apostrophe or additional quotation marks inside the string, in which case we may need to escape those punctuation(s) using a backslash ( \ ).


1 Answers

'...' 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

like image 176
kindall Avatar answered Nov 09 '22 21:11

kindall