Raw String in Python
Raw string literals in Python define normal strings that are prefixed with either an
R before the opening quote. If a backslash (
\) is in the string, the raw string treats this character as a literal character but not an escape character.
It is required to double every backslash when defining a string so that it is not mistaken as the beginning of an escape sequence like a new-line, or the new-tab. We see such syntax application in the syntax of regular expressions and when expressing Windows file paths.
r'\'will raise a syntax error because
rtreats the backslash as a literal. Without the
rprefix, the backslash is treated as an escape character.
text = "Hello\nWorld" print(text)
Without the raw string flag
r, the backslash is treated as an escape character, so when the above string is printed, the new line escape sequence is generated. Hence the two strings in the text are printed out on separate lines, as displayed in the output.
Using the same text example, add the
r prefix before the string.
text = r"Hello\nWorld" print(text)
From the output, the raw string flag treats the backslash as a literal and prints out the text with the backslash included. So, the input and output are both the same because the backslash character is not escaped.
r'\n' have the same value.
Python Unicode String
Unicode is one way of storing python strings. Unicode can store strings from all language types. The second way is the
ASCII type of string storage represented as
str in Python.
str is the default data type to store strings in Python.
To convert a string to Unicode type, put a
u before the text like this -
u'string' or call the
unicode() function like this -
u'text'is a Unicode string while
textis a byte string. A Unicode object takes more memory space.
test = u"一二三" print(test)