总的来说
- str():将传入的值转换为适合人阅读的字符串形式
- repr():将传入的值转换为 Python 解释器可读取的字符串形式
传入整型
# numberresp = 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\'> 11.1 <class \'str\'> 31 <class \'str\'> 11.1 <class \'str\'> 3
传入字符串
# stringresp = 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