OOP – String Representation (__str__)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

OOP – String Representation (__str__)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does __str__ control?
A How to create an object
B What print(object) displays
2. When is __str__ called?
A When you print() or str() the object
B When you create the object
3. What must __str__ return?
A A string
B None
4. What does print(obj) show without __str__?
A Nothing
B Something like <ClassName object at 0x...>
5. Does __str__ change the object?
A No it just returns a string description
B Yes

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def __str__(self): return 42 AOutput: 3.14
2def Str(self): BOk: Returns a formatted string from __str__
3def __str__(self): CValid: Defines string representation of an object
4return f'Dog: {self.name}' DOutput: [1, 2]
5str(3.14) EError: __str__ must return a string
6str([1, 2]) FError: Magic method must be __str__

Definitions

__str__(self)

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
class Dog: def _______(self): return 'Dog: ' + self.name
2.
class Dog: def _______(self): ______ self.name
3.
class Dog: def __str__(self): return 'Dog: ' + ____.name
4.
dog = Dog('Rex') _____(dog)
5.
class Point: def _______(self): return '(' + str(____.x) + ')'

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Dog:
B def __str__(self):
C print('Dog: ' + self.name)
Question 2
A class Dog:
B def __str__(self):
C return 42
Question 3
A class Dog:
B def __str__():
C return 'Dog'
Question 4
A class Dog:
B def _str_(self):
C return 'Dog'
Question 5
A class Dog:
B def str(self):
C return 'Dog'

Answer Key

OOP – String Representation (__str__)
Section A: Quick Questions
Q1
B — What print(object) displays
Q2
A — When you print() or str() the object
Q3
A — A string
Q4
B — Something like <ClassName object at 0x...>
Q5
A — No it just returns a string description
Section B: Match the Pairs
Q1
def __str__(self): return 42 → Error: __str__ must return a string
Q2
def Str(self): → Error: Magic method must be __str__
Q3
def __str__(self): → Valid: Defines string representation of an object
Q4
return f'Dog: {self.name}' → Ok: Returns a formatted string from __str__
Q5
str(3.14) → Output: 3.14
Q6
str([1, 2]) → Output: [1, 2]
Section C: Definitions
Q1
__str__(self) — Returns a human-readable string used by print() and str().~~__repr__(self)
Section D: Fill in the Blanks
Q1
class Dog: def __str__(self): return 'Dog: ' + self.name
Q2
class Dog: def __str__(self): return self.name
Q3
class Dog: def __str__(self): return 'Dog: ' + self.name
Q4
dog = Dog('Rex') print(dog)
Q5
class Point: def __str__(self): return '(' + str(self.x) + ')'
Section E: Spot the Error
Q1
C — __str__ must RETURN a string, not print it
Q2
C — __str__ must return a string, not an integer
Q3
B — Missing self parameter
Q4
B — Need double underscores: __str__ not _str_
Q5
B — Must be __str__ with double underscores not just str