W3docs

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

python— editable, runs on the server

<div class="alert alert-info flex not-prose"> Watch a course <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

python— editable, runs on the server

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

python— editable, runs on the server