Python ascii() 使用方法及示例

由网友 大卫 发布 阅读 5

Python ascii() 使用方法及示例

Python 内置函数

ascii()方法返回一个字符串,其中包含对象的可打印表示形式。它使用\x,\u或\U转义符转义字符串中的非ASCII字符。

ascii()的语法为:

ascii(object)

ascii()参数

ascii()方法采用一个对象(例如:字符串列表等)。

ascii()返回值

它返回一个包含对象可打印表示形式的字符串。

例如,ö更改为\xf6n,√更改为\u221a

字符串中的非ASCII字符使用\x,\u或\U进行转义。

示例1:ascii()方法如何工作?

normalText = 'Python is interesting'
print(ascii(normalText))

otherText = 'Pythön is interesting'
print(ascii(otherText))

print('Pyth\xf6n is interesting')

运行该程序时,输出为:

'Python is interesting'
'Pyth\xf6n is interesting'
Pythön is interesting

更多实例

randomList = ['Python', 'Pythön', 5]
print(ascii(randomList))

运行该程序时,输出为:

['Python', 'Pyth\xf6n', 5]

Python 内置函数

Python bool() Python bin()