1. 程式人生 > >str() vs repr() in Python

str() vs repr() in Python

一個 使用 clas lan 精度 org 表示 tps 對象

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/

str() vs repr() in Python