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

64,965

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:

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

Share:
64,965
davidmytton
Author by

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, 2022

Comments

  • davidmytton
    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
    nosklo over 15 years
    in fact there is a fourth way: '''string'''
  • retgoat
    retgoat over 15 years
    and triple quoted strings are primarily meant for multiline comments.
  • Russell Smith
    Russell Smith over 9 years
    Your 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
    Martin Tournoij over 9 years
    Generated bytecode also seems the same ... Which is exactly what the language reference says it should be.
  • gseattle
    gseattle over 9 years
    What version Bryan? I'm on 2.7.6
  • gseattle
    gseattle over 9 years
    Thx. 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
    touch my body over 8 years
    you should delete this, before the StackOverflow elitist gestapo show up in droves and downvote your post
  • hotzen
    hotzen over 7 years
    i liked to hear "the widely used practice", sounds sensible
  • honza_p
    honza_p over 6 years
    It prefers single quotes unless single quotes are in the string itself. Then it switches to double quotes. IPython does exactly the opposite.