Python ascii() 函数
本帖最后由 一个账号 于 2020-3-20 14:46 编辑Python ascii() 函数
语法
ascii(obj, /)
参数
参数描述
obj对象
描述
ascii() 函数用于是返回一个表示对象的字符串,类似于 repr() 函数。
不同的是如果对象中包含了非 ASCII 字符串的话,则返回通过 repr() 函数使用 \x, \u 或 \U 编码的字符。
返回值
返回字符串。
例子
>>> ascii(123)
'123'
>>> ascii("小甲鱼")
"'\\u5c0f\\u7532\\u9c7c'"
>>> ascii("abc")
"'abc'"
>>> ascii({1, 5, 3})
'{1, 3, 5}'
>>> ascii({1 : 1, 2 : 2, 3 : 3})
'{1: 1, 2: 2, 3: 3}'
>>> class A:
def __repr__(self):
return "Hello World!"
>>> a = A()
>>> ascii(a)
'Hello World!'
>>> class A:
def __repr__(self):
return "中文字符"
>>> a = A()
>>> ascii(a)
'\\u4e2d\\u6587\\u5b57\\u7b26'
页:
[1]