repr(object) 'official' string representation of an object

returns the string data type.

Using set object

We used one set object here.
We can use any type of object and get the string representation of the same.
print(type(my_data))  #<class 'set'>
print(type(my_data2)) #<class 'str'>
print(repr(my_data))  #{5,6,2,4}
<class 'set'>
<class 'str'>
{2, 4, 5, 6}

Using eval()

By using eval() we can get back the object. In above code my_data2 is an string object, using this we can create our original set object.
<class 'set'>

Using str()

Our built in function str() also returns the string from the object but here we get the 'informal' or nicely printable string representation of an object. Such returned string may not work with eval().

However repr() is typically used for debugging, so it is important that the representation is information-rich and unambiguous. See this example.
my_str = """Hi"""
print(repr(my_str)) # Output: 'Hi'
print(str(my_str))  # Output: Hi
#eval(str(my_str) == my_str)  # Gives a SyntaxError 
print(eval(repr(my_str)) == my_str) # Output: True

Example using Date object

We created one date object to get the present date and time. Check the difference in output of str() and repr()
import datetime
today =
print(str(today))  # Output: '2022-05-03 18:36:44.459740'
print(repr(today)) # Output: 'datetime.datetime(2022, 5, 3, 18, 36, 44, 459740)'
2022-05-03 18:36:44.459740
datetime.datetime(2022, 5, 3, 18, 36, 44, 459740)

All Built in Functions in Python dir() compile() exec() eval()
Subscribe to our YouTube Channel here


* indicates required
Subscribe to plus2net

    Post your comments , suggestion , error , requirements etc here

    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2024 All rights reserved worldwide Privacy Policy Disclaimer