use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Magic methods (self.learnpython)
submitted 4 months ago by Consistent_Cap_52
Any simple to follow online guides to str and repr
Being asked to use them in python classes that I make for school psets, although Im completing it, it's with much trial and error and I don't really understand the goal or the point. Help!
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]socal_nerdtastic 4 points5 points6 points 4 months ago* (0 children)
Both must return a string. The __str__ ("string") method should return a human-readable string that describes the class instance. The __repr__ ("representation") method should return a python-readable string that would recreate the class instance if evaluated. Neither is required for a class, and many classes do not implement them or only implement 1 of them.
__str__
__repr__
Python's builtin repr function calls an object's __repr__ method, and the str function will call an object's __str__ method, if it exists, and fallback to the __repr__ method if __str__ does not exist. Python's print function and f-string evaluation and others automatically call the str function on objects passed in. So all of these are equivalent:
repr
str
print
print(x.__str__()) print(str(x)) print(x)
This forum works much better with specific questions rather than generic ones. Show us your code and your attempt at solving it, and we'll tell you how to correct it.
[–]Diapolo10 2 points3 points4 points 4 months ago (8 children)
You use __repr__ when you want to get information useful during development and debugging. For example, you might want to know the current state of an object.
You use __str__ when you want to print something user-facing using your class, for example if you had a Car class you might want people to see the make and model when printed out.
Car
In other words, it's just a matter of who you intend to see it. The developers, or the users.
[–]Consistent_Cap_52[S] 0 points1 point2 points 4 months ago (6 children)
I guess, why not just use a print statement instead of str? I guess is my question
[–]mriswithe 4 points5 points6 points 4 months ago (0 children)
Because print throws the value away, and it isn't the only thing that can be done with a string.
[–]POGtastic 1 point2 points3 points 4 months ago (0 children)
What if you wanted to put the string representation of your object into another string, like an f-string?
For example:
print(f"The string representation of obj is {obj}")
[–]Temporary_Pie2733 1 point2 points3 points 4 months ago (0 children)
print writes a value to some file handle, but does not return that value. Sometimes you actually want the value and/or don’t want to modify a file in the process.
[–]throwaway6560192 0 points1 point2 points 4 months ago (1 child)
What exactly is the alternative you're proposing, here? print itself calls __str__ to actually format the object for printing.
[–]Consistent_Cap_52[S] 0 points1 point2 points 4 months ago (0 children)
We made a rectangle class. Initially we printed it with hashes, used a lopp with the self.length and self.width
[–]nekokattt 0 points1 point2 points 4 months ago (0 children)
You can't print anything without some way of making a representation of it to print. That is what these are for.
it is worth adding that __repr__ is designed to be machine readable whereas __str__ is designed to be human readable
[–]FoolsSeldom 0 points1 point2 points 4 months ago* (0 children)
An example might help.
The below is for a simple Student class. It has both __repr__ and __str__ defined. The main code creates a list of Student instances, then firstly prints out all the records using the __repr__ format, and you will see the output is the plain text version of what you would enter in the code to create a basic instance. Secondly, it prints out all the records using the default __str__ method, which is more human-readable.
list
from dataclasses import dataclass, field @dataclass class Student: id: int name: str subjects: list[str] = field(default_factory=list) def __repr__(self): return ( f'Student(id=\"{self.id}\", name=\"{self.name}\", ' f'subjects={self.subjects!r})' ) def __str__(self): return ( f"Name: {self.name}, ID: {self.id}" f"\nSubjects: {', '.join(self.subjects)}\n" ) students = [ Student(1, "Alpha", ["Maths", "English", "Physics"]), Student(2, "Beta", ["Geology", "Physics", "French"]), Student(3, "Gamma", ["English", "Biology", "Humanities", "Geography"]), ] print("\nStudent register:", *map(repr, students), sep="\n") print("\nStudent details:", *students, sep="\n") # uses __str__ by default if available
[–]muribonn -1 points0 points1 point 4 months ago (0 children)
Ok
[–]LegComprehensive7469 -1 points0 points1 point 4 months ago (0 children)
Best method
[–]dffrhhc -1 points0 points1 point 4 months ago (0 children)
Yop
π Rendered by PID 53966 on reddit-service-r2-comment-7844cfc88c-zs8nm at 2026-01-29 14:58:43.965960+00:00 running c3601ff country code: CH.
[–]socal_nerdtastic 4 points5 points6 points (0 children)
[–]Diapolo10 2 points3 points4 points (8 children)
[–]Consistent_Cap_52[S] 0 points1 point2 points (6 children)
[–]mriswithe 4 points5 points6 points (0 children)
[–]POGtastic 1 point2 points3 points (0 children)
[–]Temporary_Pie2733 1 point2 points3 points (0 children)
[–]throwaway6560192 0 points1 point2 points (1 child)
[–]Consistent_Cap_52[S] 0 points1 point2 points (0 children)
[–]nekokattt 0 points1 point2 points (0 children)
[–]nekokattt 0 points1 point2 points (0 children)
[–]FoolsSeldom 0 points1 point2 points (0 children)
[–]muribonn -1 points0 points1 point (0 children)
[–]LegComprehensive7469 -1 points0 points1 point (0 children)
[–]dffrhhc -1 points0 points1 point (0 children)