While asking this question, I realized I didn't know much about raw strings. For somebody claiming to be a Django trainer, this sucks.
I know what an encoding is, and I know what u''
alone does since I get what is Unicode.
But what does r''
do exactly? What kind of string does it result in?
And above all, what the heck does ur''
do?
Finally, is there any reliable way to go back from a Unicode string to a simple raw string?
Ah, and by the way, if your system and your text editor charset are set to UTF-8, does u''
actually do anything?
Raw String Literal in C++ A Literal is a constant variable whose value does not change during the lifetime of the program. Whereas, a raw string literal is a string in which the escape characters like ' \n, \t, or \” ' of C++ are not processed. Hence, a raw string literal that starts with R”( and ends in )”.
In Python, strings prefixed with r or R , such as r'...' and r"..." , are called raw strings and treat backslashes \ as literal characters. Raw strings are useful when handling strings that use a lot of backslashes, such as Windows paths and regular expression patterns.
In Python strings, the backslash "\" is a special character, also called the "escape" character. It is used in representing certain whitespace characters: "\t" is a tab, "\n" is a newline, and "\r" is a carriage return.
The prefix 'u' in front of the quote indicates that a Unicode string is to be created. If you want to include special characters in the string, you can do so using the Python Unicode-Escape encoding.
There's not really any "raw string"; there are raw string literals, which are exactly the string literals marked by an 'r'
before the opening quote.
A "raw string literal" is a slightly different syntax for a string literal, in which a backslash, \
, is taken as meaning "just a backslash" (except when it comes right before a quote that would otherwise terminate the literal) -- no "escape sequences" to represent newlines, tabs, backspaces, form-feeds, and so on. In normal string literals, each backslash must be doubled up to avoid being taken as the start of an escape sequence.
This syntax variant exists mostly because the syntax of regular expression patterns is heavy with backslashes (but never at the end, so the "except" clause above doesn't matter) and it looks a bit better when you avoid doubling up each of them -- that's all. It also gained some popularity to express native Windows file paths (with backslashes instead of regular slashes like on other platforms), but that's very rarely needed (since normal slashes mostly work fine on Windows too) and imperfect (due to the "except" clause above).
r'...'
is a byte string (in Python 2.*), ur'...'
is a Unicode string (again, in Python 2.*), and any of the other three kinds of quoting also produces exactly the same types of strings (so for example r'...'
, r'''...'''
, r"..."
, r"""..."""
are all byte strings, and so on).
Not sure what you mean by "going back" - there is no intrinsically back and forward directions, because there's no raw string type, it's just an alternative syntax to express perfectly normal string objects, byte or unicode as they may be.
And yes, in Python 2.*, u'...'
is of course always distinct from just '...'
-- the former is a unicode string, the latter is a byte string. What encoding the literal might be expressed in is a completely orthogonal issue.
E.g., consider (Python 2.6):
>>> sys.getsizeof('ciao') 28 >>> sys.getsizeof(u'ciao') 34
The Unicode object of course takes more memory space (very small difference for a very short string, obviously ;-).
There are two types of string in python: the traditional str
type and the newer unicode
type. If you type a string literal without the u
in front you get the old str
type which stores 8-bit characters, and with the u
in front you get the newer unicode
type that can store any Unicode character.
The r
doesn't change the type at all, it just changes how the string literal is interpreted. Without the r
, backslashes are treated as escape characters. With the r
, backslashes are treated as literal. Either way, the type is the same.
ur
is of course a Unicode string where backslashes are literal backslashes, not part of escape codes.
You can try to convert a Unicode string to an old string using the str()
function, but if there are any unicode characters that cannot be represented in the old string, you will get an exception. You could replace them with question marks first if you wish, but of course this would cause those characters to be unreadable. It is not recommended to use the str
type if you want to correctly handle unicode characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With