Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

Raw string literal

The r prefix in r'...' preserves backslashes instead of interpreting escape sequences such as \n. The result remains an ordinary str object.

Version: Python 3.14; repository image, production runtime not attested

Syntax

r'...'
r"..."
R'...'

The notation r'...' creates a raw string literal. The r or R prefix must immediately precede the quote; double or triple quotes also work. The result is a str, not a separate type.

In r'\n', there are two characters: a backslash and n. The ordinary literal '\n' contains one newline character. This notation makes text containing backslashes easier to write.

A raw literal cannot end in an odd number of backslashes. A backslash can prevent a quote from closing the literal, but remains in the value: r'\'' contains a backslash and a single quote.

Examples

text = r'\n'
assert text == '\\n'
print(text)
print(len(text))
print(len('\n'))

Result

\n
2
1

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources