Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn repr and str | Magic Methods
In-Depth Python OOP

bookrepr and str

1234567
class User: def __init__(self, username): self.username = username instance = User("top.user.123") print(instance)
copy
12345678910
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" instance = User("top.user.123") print(instance)
copy
12345678910111213
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" def __str__(self): return f"This is a user {self.username}" instance = User("top.user.123") print(instance)
copy
1234567891011121314
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" def __str__(self): return f"{self.username}" instance = User("top.user.123") string = str(instance) + " is the best user!" print(string)
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 2.78

bookrepr and str

Swipe to show menu

1234567
class User: def __init__(self, username): self.username = username instance = User("top.user.123") print(instance)
copy
12345678910
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" instance = User("top.user.123") print(instance)
copy
12345678910111213
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" def __str__(self): return f"This is a user {self.username}" instance = User("top.user.123") print(instance)
copy
1234567891011121314
class User: def __init__(self, username): self.username = username def __repr__(self): return f"User: {self.username}" def __str__(self): return f"{self.username}" instance = User("top.user.123") string = str(instance) + " is the best user!" print(string)
copy

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 5. ChapterΒ 2
some-alt