Python Reference Library →

Python repr() Function – Get String Representation of an Object

Let’s look at repr() function – get string representation of an object

  1. Notice

    repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
text = 'tea'
print(repr(text))
Live state

Top-level scope

text→Object 1
Object 1 · str'tea'
This step

repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.

Picture repr() function – get string representation of an object

repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.

Image brief

Alt text
A diagram for Python repr() Function – Get String Representation of an Object. Instructions: text = 'tea' print(repr(text)). Displayed result: 'tea'. The final example bindings are text --> object 1 (str). object 1: 'tea'
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python repr() Function – Get String Representation of an Object". Teaching focus: repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences. Depict exactly the relationships and labels in this reference diagram: EXAMPLE INSTRUCTIONS text = 'tea' print(repr(text)) | v DISPLAYED RESULT 'tea' FINAL BINDINGS text --> object 1 (str) object 1: 'tea' Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python repr() Function – Get String Representation of an Object. Instructions: text = 'tea' print(repr(text)). Displayed result: 'tea'. The final example bindings are text --> object 1 (str). object 1: 'tea'

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
text = 'tea'
print(repr(text))
Live state

Top-level scope

text→Object 1
Object 1 · str'tea'
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
'tea'

What do you expect this program to print?

What does this example display?

text = 'tea'
print(repr(text))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

'tea'

Follow the printed values in order. The output is: 'tea'

More examples and reference

Python repr() Function

The repr() function returns a string representation of an object that can often be used to recreate the object. It's useful for debugging and logging because it gives more detail than just printing the value.

Syntax

repr(object)

Parameters:

  • object – Any Python object you want to inspect.

Returns:

  • A string that is a printable, and often evaluable, representation of the object.

Example 1: Using repr() with Strings

text = "Hello
World"
print(text)          # Output as interpreted by print
print(repr(text))    # Raw string representation
Hello
World
'Hello\nWorld'

Explanation: repr() shows the escape characters like \n instead of turning them into a newline. This helps you see the actual string content.

Example 2: Using repr() with Numbers

num = 3.14159
print(repr(num))
3.14159

With numbers, repr() behaves the same as str(), but it's more accurate for floating point values.

Example 3: Custom Objects and __repr__()

class Person:
    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return f"Person(name='{self.name}')"

p = Person("Alice")
print(p)
print(repr(p))
Person(name='Alice')
Person(name='Alice')

Explanation: You can define your own __repr__() method to control how objects are displayed with repr().

Use Case: Debugging

When you're logging or printing complex data structures, repr() helps you see the full raw format, which is better for developers to inspect values and structure.

repr() vs str()

FunctionPurpose
str()Returns a readable version (for end users).
repr()Returns an official, raw representation (for developers).

Common Mistakes

  • Using repr() for display when str() is more readable for users.
  • Forgetting to define __repr__() in custom classes — making debugging harder.

Interview Tip

If asked how to get the “developer version” of an object’s string, say repr(). It’s also used internally by the interpreter when displaying objects in the shell.

Summary

  • repr() gives a detailed, unambiguous string representation of objects.
  • Ideal for debugging, logging, and understanding what's really inside an object.
  • You can customize its behavior in classes using __repr__().

Practice Problem

Create a class Book with attributes title and author. Override __repr__() to return something like: Book(title='...', author='...').

class Book:
    def __init__(self, title, author):
        self.title = title
        self.author = author

    def __repr__(self):
        return f"Book(title='{self.title}', author='{self.author}')"

b = Book("1984", "George Orwell")
print(repr(b))

What to remember

Connect what you noticed in the example with the idea below.

Remember

repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.

Watch out for this idea

repr() always produces the same text as print() of the object.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →