str () versus repr () in Python

Python Methods and Functions

str () and repr () are both used to get a string representation of an object.

  1. Example str ():

    s = 'Hello, Geeks.'

    print str (s)

    print str ( 2.0 / 11.0 )

    Output:

     Hello, Geeks. 0.181818181818 
  2. ,

  3. Example repr ():

    s = 'Hello, Geeks.'

    print repr (s)

    print repr ( 2.0 / 11.0 )

    Output:

     'Hello, Geeks.' 0.18181818181818182 

Based on the above output, we can see that if we print a string using the repr () function, it prints with a couple of quotes, and if we evaluate the value, we get a more accurate value than the str () function.

Below are the differences:

  • str () is used to generate output for the end user and repr () is mainly used for debugging and development. Repr target — be unambiguous, and str — readable . For example, if we suspect the float has a slight rounding error, repr will show us, and str — no.
  • repr () calculates the "official" string the object view (the view containing all the information about the object ) and str () is used to calculates an "informal" string representation of an object (a representation that is useful for printing an object).
  • The print statement and the str () built-in function use __str__ to display the string representation of an object, and the repr () built-in function uses __repr__ to display the object.

Let's understand this with an example: —

import datetime

today = datetime.datetime.now ()

  
# Prints human readable format for date and time object

print str (today)

 
# prints the official format of the date and time object

print repr (today) 

Output:

 2016-02-22 19:32 : 04.078030 datetime.datetime (2016, 2, 22, 19, 32, 4, 78030) 

str () displays today's date so that the user can understand the date and time.

repr () prints the "official" representation of the date and time object (hence, using the "official" string (we can reverse engineer the object).

How do we make them work for our own defined classes?
The custom class must also have __repr__ if we want details for debugging. And if we think it would be useful to have a string version for users, we create a __str__ function.

# Python program to demonstrate writing __repr__ and
# __str__ for custom classes

 
# Custom class for representing complex numbers

class Complex :

 

# Constructor

def __ init __ ( self , real, imag):

  self . real = real

self . imag = imag

 

# To call repr (). Prints information about an object

def __ repr__ ( self ):

return 'Rational (% s,% s)' % ( self . real, self . imag) 

 

# To call str (). Prints human readable form

def __ str __ ( self ):

  return '% s + i% s' % ( self . real, self . imag) 

 

 
# Driver program for testing above

t = Complex ( 10 , 20 )

 

print str (t)  # That same as & quot; print t & quot;

print repr (t)

Output:

 10 + i20 Rational (10, 20) 

This article courtesy of Arpit Agarwal. If you like Python.Engineering and would like to contribute your contribution, you can also write an article and mail it to [email protected] See your article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or would like to share more information on the topic under discussion





Tutorials