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

PythonStringQuotesDouble Quotes

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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

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 - Python

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 - Python

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 - Python

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

Solution 6 - Python

The difference between " and ' string quoting is just in style - except that the one removes the need for escaping the other inside the string content.

Style

PEP8 recommends a consistent rule, PEP257 suggests that docstrings use triple double quotes.

> In Python, single-quoted strings and double-quoted strings are the > same. This PEP does not make a recommendation for this. Pick a rule > and stick to it. When a string contains single or double quote > characters, however, use the other one to avoid backslashes in the > string. It improves readability. > > For triple-quoted strings, always use double quote characters to be > consistent with the docstring convention in PEP 257 .

Widely used however is the practice to prefer double-quotes for natural language strings (including interpolation) - thus anything which is potentially candidate for I18N. And single quotes for technical strings: symbols, chars, paths, command-line options, technical REGEXes, ...

(For example, when preparing code for I18N, I run a semi-automatic REGEX converting double quoted strings quickly for using e.g. gettext)

Solution 7 - Python

There are 3 ways you can qoute strings in python: "string" 'string' """ string string """ they all produce the same result.

Solution 8 - Python

There is no difference in Python, and you can really use it to your advantage when generating XML. Correct XML syntax requires double-quotes around attribute values, and in many languages, such as Java, this forces you to escape them when creating a string like this:

String HtmlInJava = "<body bgcolor=\"Pink\">"

But in Python, you simply use the other quote and make sure to use the matching end quote like this:

html_in_python = '<body bgcolor="Pink">'

Pretty nice huh? You can also use three double quotes to start and end multi-line strings, with the EOL's included like this:

multiline_python_string = """
This is a multi-line Python string which contains line breaks in the 
resulting string variable, so this string has a '\n' after the word
'resulting' and the first word 'word'."""

Solution 9 - Python

Yes. Those claiming single and double quotes are identical in Python are simply wrong.

Otherwise in the following code, the double-quoted string would not have taken an extra 4.5% longer for Python to process:

import time

time_single = 0
time_double = 0

for i in range(10000000):
	# String Using Single Quotes
	time1 = time.time()
	str_single1 = 'Somewhere over the rainbow dreams come true'
	str_single2 = str_single1
	time2 = time.time()
	time_elapsed = time2 - time1
	time_single += time_elapsed

	# String Using Double Quotes 
	time3 = time.time()
	str_double1 = "Somewhere over the rainbow dreams come true"
	str_double2 = str_double1
	time4 = time.time()
	time_elapsed = time4 - time3
	time_double += time_elapsed
	
print 'Time using single quotes: ' + str(time_single)
print 'Time using double quotes: ' + str(time_double)

Output:

>python_quotes_test.py
Time using single quotes: 13.9079978466
Time using double quotes: 14.5360121727

So if you want fast clean respectable code where you seem to know your stuff, use single quotes for strings whenever practical. You will also expend less energy by skipping the shift key.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondavidmyttonView Question on Stackoverflow
Solution 1 - PythonMilen A. RadevView Answer on Stackoverflow
Solution 2 - PythonBryan OakleyView Answer on Stackoverflow
Solution 3 - PythonBruno GomesView Answer on Stackoverflow
Solution 4 - PythonefotinisView Answer on Stackoverflow
Solution 5 - PythonThorstenView Answer on Stackoverflow
Solution 6 - PythonkxrView Answer on Stackoverflow
Solution 7 - PythonVasilView Answer on Stackoverflow
Solution 8 - PythonSteve MoyerView Answer on Stackoverflow
Solution 9 - PythongseattleView Answer on Stackoverflow