read: Python Backslash (escaping character) and Python Escaping Sequences
Definition
In Python, when you prefix a string with the letter r
or R
such as r'...'
and R'...'
, that string becomes a raw string. Unlike a regular string, a raw string treats the backslashes (\
) as literal characters.
Raw strings are useful when you deal with strings that have many backslashes, for example directory paths on Windows.
Example:
Output:
Output:
lang ver
Python 3
However, raw strings treat the backslash (\
) as a literal character. For example:
Convert a regular string into a raw string
To convert a regular string into a raw string, you use the built-in repr() function. For example: