ascii()方法返回一個(gè)字符串,其中包含對(duì)象的可打印表示形式。它使用\x,\u或\U轉(zhuǎn)義符轉(zhuǎn)義字符串中的非ASCII字符。
ascii()的語(yǔ)法為:
ascii(object)
ascii()方法采用一個(gè)對(duì)象(例如:字符串,列表等)。
它返回一個(gè)包含對(duì)象可打印表示形式的字符串。
例如,?更改為\xf6n,√更改為\u221a
字符串中的非ASCII字符使用\x,\u或\U進(jìn)行轉(zhuǎn)義。
normalText = 'Python is interesting' print(ascii(normalText)) otherText = 'Pyth?n is interesting' print(ascii(otherText)) print('Pyth\xf6n is interesting')
運(yùn)行該程序時(shí),輸出為:
'Python is interesting' 'Pyth\xf6n is interesting' Pyth?n is interesting
randomList = ['Python', 'Pyth?n', 5] print(ascii(randomList))
運(yùn)行該程序時(shí),輸出為:
['Python', 'Pyth\xf6n', 5]