Python ascii()函数

描述:

Python ascii()函数和repr() 函数有点类似,返回一个表示对象的字符串, 但是对于字符串中的非 ASCII 字符则返回通过 repr() 函数使用 \x, \u 或 \U 编码的字符。 生成类似 Python2 版本中 repr() 函数返回的字符串。

语法:

ascii(object)

参数介绍:

object---对象。可以是元组、列表、字典、字符串、set()创建的集合。

返回值:

返回字符串

下面例子展示ascii()函数使用方法
 

print(ascii((1,2))) #元组
print(type(ascii((1,2))))

print(ascii([1,2])) #列表
print(type(ascii([1,2])))

print(ascii({1:2,'name':5})) #字典
print(type(ascii({1:2,'name':5})))

print(ascii('?')) #字符串,非 ASCII字符,转义
print(type(ascii("?")))

print(ascii(set([1,1,2,3]))) #集合,由set()创建
print(type(ascii(set([1,1,2,3]))))

输出

(1, 2)
<class 'str'>
[1, 2]
<class 'str'>
{1: 2, 'name': 5}
<class 'str'>
'\U0001f60a'
<class 'str'>
{1, 2, 3}
<class 'str'>

引申:可以对比学 python repr()函数

区别:与repr()函数不同的是,ascii() 函数在获取repr()函数的返回值之后,会使用转义序列 (\x, \u , \U) 来表示其中的非 ASCII码字符(上面的例子)。

本期ascii()函数就学到这里。

  • 6
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

软件测试李同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值