Python repr()

abi_mead's avatar
Published Jun 2, 2023
Contribute to Docs

The repr() function returns a printable string describing the object that is passed in. In other words, it returns the string representation passed implicitly to the eval() function.

A class can control what this function returns for its instances when called by defining a __repr__() dunder method.

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours

Syntax

The repr() function accepts a single object as parameter.

repr(object)

Codebyte Example

Below, two instances of repr() are called:

  • The first one returns the default value (a string with the name and address of the object, enclosed in angle brackets).
  • The second defines the output string and implements the __repr__()method.
Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy

  • Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
    • Includes 6 Courses
    • With Professional Certification
    • Beginner Friendly.
      75 hours
  • Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
    • With Certificate
    • Beginner Friendly.
      24 hours