0% found this document useful (0 votes)
50 views2 pages

Difference Between 'Repr ', 'STR ', and 'Print ' in Python

The document explains the differences between repr(), str(), and print() functions in Python. repr() provides an unambiguous string representation for debugging, str() offers a user-friendly output, and print() outputs a human-readable format by calling str(). Examples with a custom class illustrate how these functions behave and their intended purposes.

Uploaded by

vidhikapoor1298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

Difference Between 'Repr ', 'STR ', and 'Print ' in Python

The document explains the differences between repr(), str(), and print() functions in Python. repr() provides an unambiguous string representation for debugging, str() offers a user-friendly output, and print() outputs a human-readable format by calling str(). Examples with a custom class illustrate how these functions behave and their intended purposes.

Uploaded by

vidhikapoor1298
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Difference Between repr(), str(), and print() in Python

1. repr() (Representa on of an Object)

 Returns an unambiguous string representa on of an object.

 The goal is to generate a string that can be used to recreate the object.

 Used for debugging and development.

 If a class defines __repr__(), repr() calls that method.

2. str() (String Representa on)

 Returns a user-friendly string representa on of an object.

 The goal is to make the output readable for users.

 Calls __str__() if defined in the class; otherwise, falls back to __repr__().

3. print() (Output to Console)

 Prints a human-readable representa on of the object.

 Internally calls str(), meaning it favors __str__() if available.

Example to Understand Differences

import date me

today = date me.date me.now()

print(str(today)) # Output: '2025-03-25 20:05:30.123456' (User-friendly)

print(repr(today)) # Output: 'date me.date me(2025, 3, 25, 20, 5, 30, 123456)' (Recreate
object)

print(today) # Calls str() internally, same as str(today)

Example with a Custom Class

class Book:

def __init__(self, tle, author):

self. tle = tle


self.author = author

def __repr__(self):

return f"Book('{self. tle}', '{self.author}')"

def __str__(self):

return f"'{self. tle}' by {self.author}"

book = Book("Python Tricks", "Dan Bader")

print(str(book)) # Output: 'Python Tricks' by Dan Bader

print(repr(book)) # Output: Book('Python Tricks', 'Dan Bader')

print(book) # Same as str(book), prints: 'Python Tricks' by Dan Bader

Key Differences

Feature repr() str() print()

Display
Purpose Debugging (Unambiguous) Readability (User-friendly)
Output

__str__(), falls back to


Calls __repr__() Uses str()
__repr__()

Example Book('Python Tricks', 'Dan


'Python Tricks' by Dan Bader Same as str()
Output Bader')

Would you like more examples?

You might also like