I’m trying to understand what the r’’ prefix does in Python and how it relates to raw string literals. I know u’’ is for Unicode, but what exactly does a python r string do? What’s the result of using r’‘, and how is it different from a regular string? Also, how does ur’’ behave, and is there a way to convert a Unicode string back to a raw form?
I’ve worked with Python for a while, especially on Windows, and honestly, the python r string
has been a lifesaver.
When writing file paths like:
path = r"C:\Users\Name\Documents"
Without the r
, Python treats things like \N
or \U
as special Unicode escapes—this breaks your path.
The r
just tells Python: “Hey, don’t mess with backslashes. Take them as-is.” Simple, but powerful.
Yep, totally agree with @apksha.shukla and to add to that, I mainly rely on python r string
in regex work. Regex is already tricky, escaping every backslash would be a nightmare.
So instead of this:
pattern = "\\d+\\.\\d+"
I use:
pattern = r"\d+\.\d+"
Cleaner and easier to read.
Also, quick note: ur""
was from Python 2. In Python 3, just use r""
, everything is Unicode by default now.
Totally with @joe-elmoufak on that, been teaching Python for 5+ years and here’s how I explain it:
Using a python r string
just means *“take it literally.”
So "\\n"
becomes a backslash and an “n”, not a newline.
But here’s the kicker: once the string is created, it’s just a normal string. There’s no way to reverse a regular string back into a raw one, it only affects how Python reads it when the code runs.
Think of r""
as just a different way to write, not store, strings.