Remember
repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.
repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
text = 'tea'
print(repr(text))Top-level scope
'tea'repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
text = 'tea'
print(repr(text))
|
v
DISPLAYED RESULT
'tea'
FINAL BINDINGS
text --> object 1 (str)
object 1: 'tea'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
text = 'tea'
print(repr(text))Top-level scope
'tea'The example has finished. Compare the final output with the values and references above.
'tea'
What do you expect this program to print?
text = 'tea'
print(repr(text))repr() FunctionThe 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.
repr(object)
object – Any Python object you want to inspect.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.
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.
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().
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.
| Function | Purpose |
|---|---|
str() | Returns a readable version (for end users). |
repr() | Returns an official, raw representation (for developers). |
repr() for display when str() is more readable for users.__repr__() in custom classes — making debugging harder.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.
repr() gives a detailed, unambiguous string representation of objects.__repr__().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))
Connect what you noticed in the example with the idea below.
repr() returns a representation useful for inspection. For strings, it shows quotes and escape sequences.
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.