From the python docs for literals: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.
Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.
and
A string literal with 'f' or 'F' in its prefix is a formatted string literal; see Formatted string literals. The 'f' may be combined with 'r', but not with 'b' or 'u', therefore raw formatted strings are possible, but formatted bytes literals are not.
So:
- r means raw
- b means bytes
- u means unicode
- f means format
The r and b were already available in Python 2, as such in many other languages (they are very handy sometimes).
Since the strings literals were not unicode in Python 2, the u-strings were created to offer support for internationalization. As of Python 3, u-strings are the default strings, so "..." is semantically the same as u"...".
Finally, from those, the f-string is the only one that isn't supported in Python 2.
Answer from Vitor SRG on Stack OverflowFrom the python docs for literals: https://docs.python.org/3/reference/lexical_analysis.html#string-and-bytes-literals
Bytes literals are always prefixed with 'b' or 'B'; they produce an instance of the bytes type instead of the str type. They may only contain ASCII characters; bytes with a numeric value of 128 or greater must be expressed with escapes.
Both string and bytes literals may optionally be prefixed with a letter 'r' or 'R'; such strings are called raw strings and treat backslashes as literal characters. As a result, in string literals, '\U' and '\u' escapes in raw strings are not treated specially. Given that Python 2.x’s raw unicode literals behave differently than Python 3.x’s the 'ur' syntax is not supported.
and
A string literal with 'f' or 'F' in its prefix is a formatted string literal; see Formatted string literals. The 'f' may be combined with 'r', but not with 'b' or 'u', therefore raw formatted strings are possible, but formatted bytes literals are not.
So:
- r means raw
- b means bytes
- u means unicode
- f means format
The r and b were already available in Python 2, as such in many other languages (they are very handy sometimes).
Since the strings literals were not unicode in Python 2, the u-strings were created to offer support for internationalization. As of Python 3, u-strings are the default strings, so "..." is semantically the same as u"...".
Finally, from those, the f-string is the only one that isn't supported in Python 2.
u-strings is for unicode in python 2. Most probably you should forget this, if you're working with modern applications — default strings are all unicode in python 3, and if you're migrating from python 2, you should most probably use
from __future__ import unicode_literals, which makes [almost] the same for python 2b-strings is for raw bytes — they have no idea of text, rather just stream of bytes. Sometimes used as input for your source, most often as result of network or low-level code — reading data in binary format, unpacking archives, working with encryption libraries etc.
Moving from/to b-string to
strdone via# python 3 >>> 'hēllö'.encode('utf-8') b'h\xc4\x93ll\xc3\xb6' >>> b'h\xc4\x93ll\xc3\xb6'.decode() 'hēllö' # python 2 without __future__ >>> u'hēllö'.encode('utf-8') 'h\xc4\x93ll\xc3\xb6' >>> 'h\xc4\x93ll\xc3\xb6'.decode('utf-8') u'h\u0113ll\xf6' # this is correct representationr-strings are not specifically for regex, those are "raw" strings. Unlike regular string literals, r-string doesn't give any special meaning for escape characters. I.e. normal string
'abc\n'is 4 characters long, last char is "newline" special character. To provide it in literal, we're using escaping with\. For raw strings,r'abc\n'is 5-length string, last two characters are literally\andn. Two places to see raw strings often are:
regex patterns — to not mess escaping with actual special characters in patters
file path notations for windows systems, as windows family uses
\as delimeter, normal string literals will look like'C:\\dir\\file', or'\\\\share\\dir', while raw would be nicer:r'C:\dir\file'andr'\\share\dir'respectively
- One more notable is f-strings, which came to life with python 3.6 as simple and powerful way of formatting strings:
f'a equals {a} and b is {b}'will substitute variablesaandbin runtime.
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,