Is there any difference between "string" and 'string' in Python? [duplicate]

No:

2.4.1. String and Bytes literals

...In plain English: Both types of literals can be enclosed in matching single quotes (') or double quotes ("). They can also be enclosed in matching groups of three single or double quotes (these are generally referred to as triple-quoted strings). The backslash (\) character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character...


Python is one of the few (?) languages where ' and " have identical functionality. The choice for me usually depends on what is inside. If I'm going to quote a string that has single quotes within it I'll use double quotes and visa versa, to cut down on having to escape characters in the string.

Examples:

"this doesn't require escaping the single quote"
'she said "quoting is easy in python"'

This is documented on the "String Literals" page of the python documentation:

  • http://docs.python.org/2/reference/lexical_analysis.html#string-literals (2.x)
  • http://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals (3.x)