According to the documentation, they're pretty much interchangeable. Is there a stylistic reason to use one over the other?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
'
="
/
=\
=\\
example :
Results are the same
=>> no, they're not the same. A single backslash will escape characters. You just happen to luck out in that example because
\k
and\w
aren't valid escapes like\t
or\n
or\\
or\"
If you want to use single backslashes (and have them interpreted as such), then you need to use a "raw" string. You can do this by putting an '
r
' in front of the stringAs far as paths in Windows are concerned, forward slashes are interpreted the same way. Clearly the string itself is different though. I wouldn't guarantee that they're handled this way on an external device though.
I used to prefer
'
, especially for'''docstrings'''
, as I find"""this creates some fluff"""
. Also,'
can be typed without the Shift key on my Swiss German keyboard.I have since changed to using triple quotes for
"""docstrings"""
, to conform to PEP 257.I'm with Will:
I'll stick with that even if it means a lot of escaping.
I get the most value out of single quoted identifiers standing out because of the quotes. The rest of the practices are there just to give those single quoted identifiers some standing room.
Personally I stick with one or the other. It doesn't matter. And providing your own meaning to either quote is just to confuse other people when you collaborate.
If the string you have contains one, then you should use the other. For example,
"You're able to do this"
, or'He said "Hi!"'
. Other than that, you should simply be as consistent as you can (within a module, within a package, within a project, within an organisation).If your code is going to be read by people who work with C/C++ (or if you switch between those languages and Python), then using
''
for single-character strings, and""
for longer strings might help ease the transition. (Likewise for following other languages where they are not interchangeable).The Python code I've seen in the wild tends to favour
"
over'
, but only slightly. The one exception is that"""these"""
are much more common than'''these'''
, from what I have seen.Your team's taste or your project's coding guidelines.
If you are in a multilanguage environment, you might wish to encourage the use of the same type of quotes for strings that the other language uses, for instance. Else, I personally like best the look of '