Is there any difference between "string" and 'string' in Python?
Solution 1
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...
Solution 2
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)
Solution 3
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"
string1
string2
=> nil
irb(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'
string1
string2
>>> print r'string1\nstring2'
string1\nstring2
Solution 4
Single and double quoted strings in Python are identical. The only difference is that single-quoted strings can contain unescaped double quote characters, and vice versa. For example:
'a "quoted" word'
"another 'quoted' word"
Then again, there are triple-quoted strings, which allow both quote chars and newlines to be unescaped.
You can substitute variables in a string using named specifiers and the locals() builtin:
name = 'John'
lastname = 'Smith'
print 'My name is %(name)s %(lastname)s' % locals() # prints 'My name is John Smith'
Solution 5
The interactive Python interpreter prefers single quotes:
>>> "text"
'text'
>>> 'text'
'text'
This could be confusing to beginners, so I'd stick with single quotes (unless you have different coding standards).
davidmytton
Co-founder of Console (the best tools for developers). Researching sustainable computing at Imperial College London & Uptime Institute. Previously Co-founder, Server Density (acquired by StackPath).
Updated on July 05, 2022Comments
-
davidmytton almost 2 years
In PHP, a string enclosed in "double quotes" will be parsed for variables to replace whereas a string enclosed in 'single quotes' will not. In Python, does this also apply?
-
nosklo over 15 yearsin fact there is a fourth way: '''string'''
-
retgoat over 15 yearsand triple quoted strings are primarily meant for multiline comments.
-
Russell Smith over 9 yearsYour testing methods are flawed and don't really represent the truth. For example, when I run your exact code on my machine, the double quote result was consistently (9 out of 10 runs) faster than the single quote result. More importantly, no matter which quoting I use first, the second result is always faster than the first (ie: I can move the double-quote code before the single quote code, and the second one to run is always faster).
-
Martin Tournoij over 9 yearsGenerated bytecode also seems the same ... Which is exactly what the language reference says it should be.
-
gseattle over 9 yearsWhat version Bryan? I'm on 2.7.6
-
gseattle over 9 yearsThx. I had also reversed the order of the sections before posting that and the single quotes were always faster, so, go figure I guess.
-
touch my body over 8 yearsyou should delete this, before the StackOverflow elitist gestapo show up in droves and downvote your post
-
hotzen over 7 yearsi liked to hear "the widely used practice", sounds sensible
-
honza_p over 6 yearsIt prefers single quotes unless single quotes are in the string itself. Then it switches to double quotes. IPython does exactly the opposite.