What exactly do "u" and "r" string prefixes do, and what are raw string literals?
In Python, the "r" prefix before a string denotes that it is a raw string literal.
In Python, the r prefix before a string denotes that it is a raw string literal. This means that backslashes (\) are treated as literal characters rather than escape characters. For example, the string "C:\Windows" contains an unrecognized escape sequence. In modern Python, it is preserved as "C:\Windows", but the r prefix guarantees this behavior and prevents version-specific warnings. Note that raw strings cannot end with an odd number of backslashes, as the final backslash would escape the closing quote.
Note: In modern Python (3.6+), invalid escape sequences like \W are preserved but raise a DeprecationWarning. Python 3.12+ raises a SyntaxWarning for unrecognized escapes, and they will become a SyntaxError in future versions.
raw string literal in Python
normal_string = "C:\Windows"
print(normal_string) # Output: C:\Windows
raw_string = r"C:\Windows"
print(raw_string) # Output: C:\Windows
<div class="alert alert-info flex not-prose">![]()
<span class="hidden md:block">Watch a video course</span>Python - The Practical Guide</div>
The u prefix before a string denotes that it is a Unicode string literal. Unicode is a standard for encoding characters in a way that can be understood by computers. In Python 3, all strings are Unicode by default, so the u prefix is not necessary.
unicode string literal in Python
unicode_string = u"Hello, 世界!"
print(unicode_string)You can also combine prefixes like ru to make a raw Unicode string. The ur prefix is invalid in Python 3 and raises a SyntaxError. Using ru is the standard approach, though it is rarely needed since all strings are Unicode by default in Python 3.
raw unicode string in Python
raw_unicode_string = ru"C:\Windows"
print(raw_unicode_string) # Output: C:\Windows