str() vs repr() in Python

str() 和 repr() 都是用作一个对象的字符表示.

1 str()的举例:

s = 'Hello, Geeks.'
print str(s)
print str(2.0/11.0)

输出结果:
Hello, Geeks.
0.181818181818

2 repr()的举例:
s = 'Hello, Geeks.'
print repr(s)
print repr(2.0/11.0)
输出结果:
'Hello, Geeks.'
0.18181818181818182

从上面结果可以看出,如果我们使用repr() 打印字符串的话,他会多一个引用.
如果做运算的话,str()会比repr()的精度低.


参考文档: https://www.geeksforgeeks.org/str-vs-repr-in-python/

猜你喜欢

转载自www.cnblogs.com/dylancao/p/9552483.html