Python基础教程:repr()与str() 的区别
生活随笔
收集整理的這篇文章主要介紹了
Python基础教程:repr()与str() 的区别
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
總的來說
- str():將傳入的值轉(zhuǎn)換為適合人閱讀的字符串形式
- repr():將傳入的值轉(zhuǎn)換為 Python 解釋器可讀取的字符串形式
傳入整型
# 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))# 輸出結(jié)果 1 <class 'str'> 1 1.1 <class 'str'> 3 1 <class 'str'> 1 1.1 <class 'str'> 3傳入字符串
''' 學(xué)習(xí)中遇到問題沒人解答?小編創(chuàng)建了一個(gè)Python學(xué)習(xí)交流QQ群:725638078 尋找有志同道合的小伙伴,互幫互助,群里還有不錯(cuò)的視頻學(xué)習(xí)教程和PDF電子書! ''' # string resp = str("test") print(resp, type(resp), len(resp))resp = repr("test") print(resp, type(resp), len(resp))# 輸出結(jié)果 test <class 'str'> 4 'test' <class 'str'> 6repr() 會(huì)在原來的字符串上面加單引號(hào),所以字符串長度會(huì) +2
總結(jié)
以上是生活随笔為你收集整理的Python基础教程:repr()与str() 的区别的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中对数组合并的方法
- 下一篇: Python面向对象中:__init__