Show what a Python raw string changes about escape sequences, with a side-by-side comparison.
A raw string is written with an r in front of the quotes, and the effect is that Python leaves every backslash exactly where you put it. The interpreter does not read the backslash as the start of an escape sequence.
normal = "line1\nline2"
raw = r"line1\nline2"
print(len(normal)) # 10
print(repr(normal)) # 'line1\nline2' - a real newline
print(len(raw)) # 12
print(repr(raw)) # 'line1\\nline2' - backslash and n
path = r"C:\Users\Ada\notes.txt"
print(path)The normal string is 10 characters long, because the escape sequence becomes a single newline character. The raw string is 12 characters, because the backslash and the letter n are two separate characters. The repr() output makes the difference easy to see, because one of them contains a real newline and the other contains the two characters backslash and n.
One thing has not changed, and it is worth knowing. The r prefix only affects backslashes. Everything else still works as usual, so quotes inside the string still need escaping, and you can combine the prefix with the f prefix as rf"..." when you need both.
The practical rule is short. A raw string is the right tool whenever the backslashes are real data and not escape instructions. Windows paths, regular expressions, and any text that will be handed to another program which interprets its own escapes all fall into that group. It also makes the code easier to read, because a reader can see the actual characters without having to decode them in their head. The previous question in this group looked at string formatting, and this one looks at the prefix that makes paths and patterns readable.