0
0
Pythonprogramming~10 mins

String representation methods in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print the string representation of the object using the __str__ method.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def __str__(self):
        return f"Person: {self.[1]"

p = Person("Alice")
print(str(p))
Drag options to blanks, or click blank then click option'
Aself
Bage
Cname
Drepr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'age' instead of 'name' which does not exist.
Using 'repr' which is a different method.
Using 'self' directly inside the f-string.
2fill in blank
medium

Complete the code to define the __repr__ method that returns a string with the class name and the name attribute.

Python
class Person:
    def __init__(self, name):
        self.name = name
    def __repr__(self):
        return f"Person(name=[1])"

p = Person("Bob")
print(repr(p))
Drag options to blanks, or click blank then click option'
Aself.name
Bname
Cself
Drepr
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' without 'self.' which causes a NameError.
Using 'repr' which is a method, not an attribute.
Using 'self' directly which is not a string.
3fill in blank
hard

Fix the error in the __str__ method to correctly return the name attribute as a string.

Python
class Animal:
    def __init__(self, species):
        self.species = species
    def __str__(self):
        return self.[1]

cat = Animal("Cat")
print(str(cat))
Drag options to blanks, or click blank then click option'
Aspecies()
Bself.species()
Cstr(species)
Dspecies
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the attribute like a function with parentheses.
Trying to convert the attribute with str() unnecessarily.
Using 'self.species()' which causes a TypeError.
4fill in blank
hard

Fill both blanks to create a __repr__ method that returns a string with the class name and the species attribute in quotes.

Python
class Animal:
    def __init__(self, species):
        self.species = species
    def __repr__(self):
        return f"Animal('[1]')"

bird = Animal("Bird")
print(repr(bird))
Drag options to blanks, or click blank then click option'
Aself.species
Bspecies
Cself.species.upper()
Drepr(self.species)
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'species' without 'self.' which causes an error.
Using 'self.species.upper()' which changes the string.
Using 'repr(self.species)' which adds extra quotes.
5fill in blank
hard

Fill all three blanks to create a class with __str__ and __repr__ methods that show the name and age attributes properly.

Python
class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age
    def __str__(self):
        return f"Name: [1], Age: [2]"
    def __repr__(self):
        return f"Person(name=[3], age={{self.age}})"

p = Person("Eve", 30)
print(str(p))
print(repr(p))
Drag options to blanks, or click blank then click option'
Aself.name
Bself.age
Dname
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'name' or 'age' without 'self.' which causes errors.
Mixing up the order of attributes in the f-string.
Using variable names that are not defined.