0
0
Pythonprogramming~20 mins

String representation methods in Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
String Representation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of __str__ vs __repr__
What is the output of the following code?
Python
class Fruit:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return f"Fruit: {self.name}"
    def __repr__(self):
        return f"Fruit('{self.name}')"

f = Fruit('Apple')
print(str(f))
print(repr(f))
A
Fruit: Apple
Apple
B
Fruit('Apple')
Fruit: Apple
C
Apple
Fruit('Apple')
D
Fruit: Apple
Fruit('Apple')
Attempts:
2 left
💡 Hint
Remember __str__ is for user-friendly display, __repr__ is for developer-friendly output.
Predict Output
intermediate
2:00remaining
Using __repr__ in a list
What is the output of this code?
Python
class Animal:
    def __init__(self, species):
        self.species = species
    def __repr__(self):
        return f"Animal('{self.species}')"

animals = [Animal('Cat'), Animal('Dog')]
print(animals)
A[Animal('Cat'), Animal('Dog')]
B['Cat', 'Dog']
C[Animal(Cat), Animal(Dog)]
D[<Animal object>, <Animal object>]
Attempts:
2 left
💡 Hint
Lists use __repr__ of their elements when printed.
🔧 Debug
advanced
2:00remaining
Fix the __str__ method to avoid error
This code raises an error when printing the object. Which option fixes the error?
Python
class Book:
    def __init__(self, title):
        self.title = title
    def __str__(self):
        return 'Book title is ' + self.title.upper

b = Book('Python 101')
print(b)
A
def __str__(self):
    return 'Book title is ' + self.title.upper()
B
def __str__(self):
    return 'Book title is ' + str(self.title.upper)
C
def __str__(self):
    return 'Book title is ' + self.title.upper
D
def __str__(self):
    return 'Book title is ' + self.title
Attempts:
2 left
💡 Hint
Check if you are calling the method or just referencing it.
🧠 Conceptual
advanced
2:00remaining
Difference between __str__ and __repr__
Which statement best describes the difference between __str__ and __repr__ methods in Python?
A<code>__str__</code> is only used when printing lists; <code>__repr__</code> is used everywhere else.
B<code>__str__</code> is for a readable string for users; <code>__repr__</code> is for unambiguous string for developers.
CBoth methods return the same string but <code>__repr__</code> is faster.
D<code>__repr__</code> is for readable string for users; <code>__str__</code> is for debugging.
Attempts:
2 left
💡 Hint
Think about who the string is for: user or developer.
Predict Output
expert
2:00remaining
Output of custom __repr__ with eval
What is the output of this code?
Python
class Point:
    def __init__(self, x, y):
        self.x = x
        self.y = y
    def __repr__(self):
        return f"Point({self.x}, {self.y})"

p = Point(3, 4)
q = eval(repr(p))
print(p == q)
print(q)
A
False
&lt;__main__.Point object at 0x...&gt;
B
True
Point(3, 4)
C
False
Point(3, 4)
D
True
&lt;__main__.Point object at 0x...&gt;
Attempts:
2 left
💡 Hint
Does eval(repr(p)) create an equal object? What about equality check?