Python - repr()、str() 的区别

时间:2021-09-12 10:49:22   收藏:0   阅读:39

总的来说

 

传入整型

# number
resp = str(1)
print(resp, type(resp), len(resp))
resp = str(1.1)
print(resp, type(resp), len(resp))

resp = repr(1)
print(resp, type(resp), len(resp))
resp = repr(1.1)
print(resp, type(resp), len(resp))


# 输出结果
1 <class str> 1
1.1 <class str> 3
1 <class str> 1
1.1 <class str> 3

 

传入字符串

# string
resp = str("test")
print(resp, type(resp), len(resp))

resp = repr("test")
print(resp, type(resp), len(resp))


# 输出结果
test <class str> 4
test <class str> 6

repr() 会在原来的字符串上面加单引号,所以字符串长度会 +2

 

原文:https://www.cnblogs.com/poloyy/p/15253032.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!