Videos
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...
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)
In Python 3.x, the correct way to check if s is a string is
isinstance(s, str)
The bytes class isn't considered a string type in Python 3.
In Python 2.x, the correct check was
isinstance(s, basestring)
basestring is the abstract superclass of str and unicode. It can be used to test whether an object is an instance of either str or unicode.
I know this is an old topic, but being the first one shown on google and given that I don't find any of the answers satisfactory, I'll leave this here for future reference:
six is a Python 2 and 3 compatibility library which already covers this issue. You can then do something like this:
import six
if isinstance(value, six.string_types):
pass # It's a string !!
Inspecting the code, this is what you find:
import sys
PY3 = sys.version_info[0] == 3
if PY3:
string_types = str,
else:
string_types = basestring,