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

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:


In some other languages, meta characters are not interpreted if you use single quotes. Take this example in Ruby:

irb(main):001:0> puts "string1\nstring2"string1string2=> nilirb(main):002:0> puts 'string1\nstring2'string1\nstring2=> nil

In Python, if you want the string to be taken literally, you can use raw strings (a string preceded by the 'r' character):

>>> print 'string1\nstring2'string1string2>>> print r'string1\nstring2'string1\nstring2