Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

repr()

Requests a representation suitable for inspection or debugging.

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

Syntax

repr(object, /)

Requests a representation suitable for inspection or debugging.

A string representation retains quotes and escapes such as a newline, making its boundaries visible in logs.

The type’s __repr__ produces inspection text. A string representation quotes the text and spells the embedded newline as a backslash and n. Printing that representation stays on one line. The original contains a, one newline and b, hence its length is three. A custom representation need not be valid Python source.

s = "a\nb"
r = repr(s)
assert len(s) == 3 and chr(92) + "n" in r and "\n" not in r

Examples

text = "a\nb"
print(repr(text))
print(len(text))

Result

'a\nb'
3

The type’s `__repr__` produces inspection text. A string representation quotes the text and spells the embedded newline as a backslash and n. Printing that representation stays on one line. The original contains a, one newline and b, hence its length is three. A custom representation need not be valid Python source.

Sources