Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1067643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:11:08+00:00 2026-05-16T20:11:08+00:00

I really don’t understand where are __str__ and __repr__ used in Python. I mean,

  • 0

I really don’t understand where are __str__ and __repr__ used in Python. I mean, I get that __str__ returns the string representation of an object. But why would I need that? In what use case scenario? Also, I read about the usage of __repr__

But what I don’t understand is, where would I use them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:11:09+00:00Added an answer on May 16, 2026 at 8:11 pm

    __repr__

    Called by the repr() built-in function and by string conversions (reverse quotes) to compute the “official” string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).

    __str__

    Called by the str() built-in function and by the print statement to compute the “informal” string representation of an object.

    Use __str__ if you have a class, and you’ll want an informative/informal output, whenever you use this object as part of string. E.g. you can define __str__ methods for Django models, which then gets rendered in the Django administration interface. Instead of something like <Model object> you’ll get like first and last name of a person, the name and date of an event, etc.


    __repr__ and __str__ are similar, in fact sometimes equal (Example from BaseSet class in sets.py from the standard library):

    def __repr__(self):
        """Return string representation of a set.
    
        This looks like 'Set([<list of elements>])'.
        """
        return self._repr()
    
    # __str__ is the same as __repr__
    __str__ = __repr__
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

There is something that I really don't understand with the HttpListener. The code below
This is one of things that I really don't get. I know that it's
To be honest with you I really don't get the relationship that Web Services
I use this code to get Server Date but I really don't understand.CreateDateTime() is
I face a very different problem that I really don't get a clue on
I really don't understand why I get a different response from xhrPost with Dojo.
I really don't understand regular expressions and was wondering what the following regular expressions
I really don't understand this. I have a simple ASP with 3 div in
Sorry for unclear subject but i really don't understand where problem. I use a
I'm new to python so I really don't know the language very well. the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.