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

In PHP, a string enclosed in double quotes will be parsed for replaceable variables, while a string enclosed in single quotes will not. In Python, is this also applicable?

+48
python string quotes double-quotes
Sep 27 '08 at 14:39
source share
9 answers

No :

2.4.1. String and byte literals

... In plain English: Both types of literals can be enclosed in single quotes ( ' ) or double quotes ( " ). They can also be enclosed in comparable groups of three single or double quotes (they are usually called triple quotes). The backslash character ( \ ) is used to delete characters that otherwise have special meaning, such as a newline, backslash, or quotation mark ...

+104
Sep 27 '08 at 14:41
source share

Python is one of the few (?) Languages ​​where "and" have identical functionality. The choice for me usually depends on what is inside. If I am going to cast a string with single quotes in it, I will use double quotes and vice versa to reduce the use of characters in the string.

Examples:

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

This is described on the "String Lines" page of the python documentation:

+30
Sep 27 '08 at 14:45
source share

In some other languages, metacharacters 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 a string to be taken literally, you can use raw strings (a string preceded by a r):

 >>> print 'string1\nstring2' string1 string2 >>> print r'string1\nstring2' string1\nstring2 
+7
Sep 27 '08 at 14:58
source share

Single and double quotes in Python are identical. The only difference is that single-quoted strings can contain unescaped double-quoted characters and vice versa. For example:

 'a "quoted" word' "another 'quoted' word" 

Then again there are strings with three quotation marks that allow both quotation marks and newlines to be non-exclusive.

You can replace the variables in a string using named specifications and locals () builtin:

 name = 'John' lastname = 'Smith' print 'My name is %(name)s %(lastname)s' % locals() # prints 'My name is John Smith' 
+2
Sep 27 '08 at 14:55
source share

The interactive Python interpreter prefers single quotes:

 >>> "text" 'text' >>> 'text' 'text' 

This can be confusing for beginners, so I will stick to single quotes (unless you have different coding standards).

+2
Jan 03 '15 at 11:25
source share

The difference between "and" string quoting is style-only, except that it eliminates the need to escape another 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 makes no recommendation for this. Choose a rule and stick to it. When a string contains single or double quote characters, however, use another to avoid backslashes in the string. This improves readability.

For triple strings, always use double quote characters according to the docstring convention in PEP 257.

Widely used, however, practice prefers double quotes for natural language strings (including interpolation) - thus, anything that is potentially a candidate for I18N. And single quotes for technical strings: characters, characters, paths, command line options, technical REGEXes, ...

(For example, when preparing code for I18N, I quickly run semi-automatic REGEX-converting double quotes for use, for example, gettext )

+2
Mar 15 '16 at 9:46
source share

There are three ways to build strings in python: "String" "String" "" string string "" they all give the same result.

0
Sep 27 '08 at 14:43
source share

There is no difference in Python, and you can take advantage of it when creating XML. The correct XML syntax requires double quotes around attribute values, and in many languages, such as Java, this forces you to avoid them when creating such a string:

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

But in Python, you just use a different quote and don't forget to use the appropriate quote quote as follows:

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

Pretty nice, huh? You can also use three double quotes to start and end multiline strings, with EOL included as follows:

 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'.""" 
0
Sep 27 '08 at 14:46
source share

Yes. Those who claim single and double quotes are identical in Python, are simply mistaken.

Otherwise, in the following code, the double-quoted string should not have 4.5% more time to process Python:

 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 need a quick, clean, respectable code where you seem to know your stuff, use single quotes for strings when it's practically possible. You will also spend less energy by skipping the shift key.

-3
Aug 27 '14 at 12:29
source share



All Articles