17.8. OOP Str
str(obj)- object stringification
Dedicated for end-users
Calling function
print(obj)callsstr(obj)Calling function
str(obj)callsobj.__str__()Calling
f'{obj}'callsobj.__str__()Calling
f'{obj!s}'callsobj.__str__()Method
obj.__str__()must returnstr
>>> class User:
... def __init__(self, firstname, lastname):
... self.firstname = firstname
... self.lastname = lastname
>>>
>>>
>>> alice = User('Alice', 'Apricot')
>>>
>>> print(alice)
<__main__.User object at 0x1087b12b0>
17.8.1. Problem
Object without
__str__()method overloaded prints their memory address
>>> class User:
... def __init__(self, firstname, lastname):
... self.firstname = firstname
... self.lastname = lastname
>>>
>>>
>>> alice = User('Alice', 'Apricot')
>>>
>>> print(alice)
<__main__.User object at 0x10aef7450>
>>>
>>> str(alice)
'<__main__.User object at 0x10aef7450>'
>>>
>>> alice.__str__()
'<__main__.User object at 0x10aef7450>'
>>>
>>> f'{alice}'
'<__main__.User object at 0x10aef7450>'
>>>
>>> f'{alice!s}'
'<__main__.User object at 0x10aef7450>'
17.8.2. Solution
Objects can verbose print if
__str__()method is present:
>>> class User:
... def __init__(self, firstname, lastname):
... self.firstname = firstname
... self.lastname = lastname
...
... def __str__(self):
... return f'{self.firstname} {self.lastname}'
...
>>> alice = User('Alice', 'Apricot')
>>>
>>> print(alice)
Alice Apricot
>>>
>>> str(alice)
'Alice Apricot'
>>>
>>> alice.__str__()
'Alice Apricot'
>>>
>>> f'Hello {alice}'
'Hello Alice Apricot'
>>>
>>> f'Hello {alice!s}'
'Hello Alice Apricot'
17.8.3. Case Study
>>> import datetime
>>>
>>> today = datetime.date(2000, 1, 2)
>>> str(today)
'2000-01-02'
17.8.4. Use Case - 1
>>> class User:
... def __init__(self, username, password):
... self.username = username
... self.password = password
...
... def __str__(self):
... return f'User {self.username}'
>>>
>>>
>>> alice = User('alice', 'secret')
>>>
>>> print(alice)
User alice
17.8.5. Assignments
# %% About
# - Name: OOP Stringification Str
# - Difficulty: easy
# - Lines: 3
# - Minutes: 2
# %% License
# - Copyright 2025, Matt Harasymczuk <matt@python3.info>
# - This code can be used only for learning by humans
# - This code cannot be used for teaching others
# - This code cannot be used for teaching LLMs and AI algorithms
# - This code cannot be used in commercial or proprietary products
# - This code cannot be distributed in any form
# - This code cannot be changed in any form outside of training course
# - This code cannot have its license changed
# - If you use this code in your product, you must open-source it under GPLv2
# - Exception can be granted only by the author
# %% English
# 1. Modify class `User`
# 2. Define new method `__str__` which returns users firstname and lastname
# example: 'Alice Apricot'
# 3. Run doctests - all must succeed
# %% Polish
# 1. Zmodyfikuj klasę `User`
# 2. Zdefiniuj nową metodę `__str__`, która zwraca imię i nazwisko użytkownika
# przykład: 'Alice Apricot'
# 3. Uruchom doctesty - wszystkie muszą się powieść
# %% Expected
# >>> alice = User('Alice', 'Apricot')
# >>> str(alice)
# 'Alice Apricot'
# %% Doctests
"""
>>> import sys; sys.tracebacklimit = 0
>>> assert sys.version_info >= (3, 9), \
'Python has an is invalid version; expected: `3.9` or newer.'
>>> from pprint import pprint
>>> from inspect import isclass
>>> assert isclass(User), \
'Object `User` has an invalid type; expected: `class`.'
>>> assert hasattr(User, '__init__'), \
'Object `User` has an invalid attribute; expected: to have an attribute `__init__`.'
>>> assert hasattr(User, '__str__'), \
'Object `User` has an invalid attribute; expected: to have an attribute `__str__`.'
>>> alice = User('Alice', 'Apricot')
>>> result = str(alice)
>>> pprint(result)
'Alice Apricot'
"""
# %% Run
# - PyCharm: right-click in the editor and `Run Doctest in ...`
# - PyCharm: keyboard shortcut `Control + Shift + F10`
# - Terminal: `python -m doctest -f -v myfile.py`
# %% Imports
# %% Types
# %% Data
# %% Result
class User:
def __init__(self, firstname, lastname):
self.firstname = firstname
self.lastname = lastname