1. 程式人生 > >Python內置函數之str()

Python內置函數之str()

級別 col enc spa read err 對象 log open

class str(object="")
class str(object=b‘‘, encoding=‘utf-8‘, errors=‘strict‘)

將其他對象轉化為字符串對象。

例子:

>>> str(123)
123
>>> str()
‘‘
>>> file = open(data.txt,rb)
>>> fb = file.read()
>>> str(fb)
"b‘\\xe5\\xa4\\xa9\\xe7\\x8e\\x8b\\xe7\\x9b\\x96\\xe5\\x9c\\xb0\\xe8\\x99\\x8e\\xef\\xbc\\x8c\\xe5
\\xb0\\x8f\\xe9\\xb8\\xa1\\xe7\\x82\\x96\\xe8\\x98\\x91\\xe8\\x8f\\x87\\xe3\\x80\\x82" >>> str(fb,utf-8) #指定編碼集 天王蓋地虎,小雞燉蘑菇。 >>> str(fb,gbk,ignore) #指定報錯級別 澶╃帇鐩栧湴铏庯紝灝忛浮鐐栬槕鑿囥 >>> file.close()

Python內置函數之str()