Python学习8_str() vs repr()

转载:https://www.geeksforgeeks.org/str-vs-repr-in-python/

str() vs repr() in Python

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

  1. Example of str():

s = 'Hello, Geeks.'

print str(s)

print str(2.0/11.0)

 

Output:

 

Hello, Geeks.

  1. Example of repr():

s = 'Hello, Geeks.'

print repr(s)

print repr(2.0/11.0)

 

Output:

'Hello, Geeks.'

0.18181818181818182

 

From above output, we can see if we print string using repr() function then it prints with a pair of quotes and if we calculate a value we get more precise value than str() function.

 

Following are differences:

  • str() is used for creating output for end user while repr() is mainly used for debugging and development. repr’s goal is to be unambiguous and str’s is to be readable. For example, if we suspect a float has a small rounding error, repr will show us while str may not.
  • repr() compute the “official” string representation of an object (a representation that has all information about the abject) and str() is used to compute the “informal” string representation of an object (a representation that is useful for printing the object).
  • The print statement and str() built-in function uses __str__ to display the string representation of the object while the repr() built-in function uses __repr__ to display the object.

Let understand this by an example:-

import datetime

today = datetime.datetime.now()

  

# Prints readable format for date-time object

print str(today)

  

# prints the official format of date-time object

print repr(today)     

Run on IDE

Output:

2016-02-22 19:32:04.078030

datetime.datetime(2016, 2, 22, 19, 32, 4, 78030)

str() displays today’s date in a way that the user can understand the date and time.

repr() prints “official” representation of a date-time object (means using the “official” string representation we can reconstruct the object).

How to make them work for our own defined classes?
A user defined class should also have a __repr__ if we need detailed information 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 of __repr__ and

# __str__ for user defined classes

  

# A user defined class to represent Complex numbers

class Complex:

  

    # Constructor

    def __init__(self, real, imag):

       self.real = real

       self.imag = imag

  

    # For call to repr(). Prints object's information

    def __repr__(self):

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

  

    # For call to str(). Prints readable form

    def __str__(self):

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

  

  

# Driver program to test above

t = Complex(10, 20)

  

print str(t)  # Same as "print t"

print repr(t)

Run on IDE

Output:

10 + i20

Rational(10, 20)

This article is contributed by Arpit Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值