python3 字符串属性(三)
maketrans 和 translate的用法(配合使用)
下面是python的英文用法解释
maketrans(x, y=None, z=None, /)
Return a translation table usable for str.translate().
If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters to Unicode ordinals, strings or None.
Character keys will be then converted to ordinals.
If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it
must be a string, whose characters will be mapped to None in the result
S.translate(table) -> str
Return a copy of the string S, where all characters have been mapped
through the given translation table, which must be a mapping of
Unicode ordinals to Unicode ordinals, strings, or None.
Unmapped characters are left untouched. Characters mapped to None
are deleted.
makestans返回一个给translate用的映射表,translate根据映射表构造新的字符串。
makestran根据参数的个数有三种使用方法:
1)一个参数情况,参数必须是字典
一个字符转换成一个字符
>>> a='qwerabc2348'
>>> d={'a':'A','q':'Q'} #转换映射表
>>> tans=str.maketrans(d) #转换为translate可以使用的映射表
>>> tans
{: 'A', : 'Q'} #translate可以使用的映射表
>>> a.translate(tans)
'QwerAbc2348' #转换后的结果
一个字符转换为多个字符
>>> d2={'a':'*A*','q':'*Q*'}
>>> tans2=str.maketrans(d2)
>>> tans2
{: '*A*', : '*Q*'}
>>> a.translate(tans2)
'*Q*wer*A*bc2348
一个字符转换为None,效果为过滤删除字符
>>> d3={'a':None,'q':None}
>>> tans3=str.maketrans(d3)
>>> tans3
{: None, : None}
>>> a.translate(tans3)
'werbc2348'
2)两个参数的情况,参数(字符串)必须长度相等。
>>> a='acbsdwf124'
>>> tans4=str.makestrans('abc','ABC')
>>> tans4=str.maketrans('abc','ABC')
>>> tans4
{: , : , : }
>>> a.translate(tans4)
'ACBsdwf124'
3)三个参数的情况,前两个参数效果和2)相同,第三个参数为要过滤删除的字符表(第三个参数都映射为None)
>>> a
'acbsdwf124'
>>> tans5=str.maketrans('abc','ABC','')
>>> tans5
{: , : , : , : None, : None, : None, : None}
>>> a.translate(tans5)
'ACBsdwf'
4)映射表中的数字为unicode编码数字
>>> ord('a') >>> chr()
'a'
python3 字符串属性(三)的更多相关文章
- python3 字符串属性(一)
python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...
- python3 字符串属性(四)
1. S.partition(sep) -> (head, sep, tail) Search for the separator sep in S, and return the part b ...
- python3字符串属性(二)
1.S.isdecimal() -> bool Return True if there are only decimal characters in S, False otherwise ...
- 彻底了解构建 JSON 字符串的三种方式
原创播客,如需转载请注明出处.原文地址:http://www.cnblogs.com/crawl/p/7701856.html 前言:JSON 是轻量级的数据交换格式,很常用,尤其是在使用 Ajax ...
- python系列四:Python3字符串
#!/usr/bin/python #Python3 字符串#可以截取字符串的一部分并与其他字段拼接var1 = 'Hello World!'print ("已更新字符串 : ", ...
- Python3字符串 详解
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...
- NSAttributedString字符串属性类
//定义一个可变字符串属性对象aStr NSMutableAttributedString *aStr = [[NSMutableAttributedString alloc]initWithStri ...
- 字符串属性使用strong的原因
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- mysql 连接命令 表管理 ,克隆表,临时表,字符串属性,设定语句间的分隔符
连接和断开连接mysql -h host -u user -p (即,连接的主机.用户名和使用的密码).断开输入QUIT (或\q)随时退出: 表管理克隆表注意:create table ... li ...
随机推荐
- oracle字符乱码的解决方法
原因分析: 客户端字符集就是为了让数据库知道我们传递过去的字符是属于哪种字符集,以便于Oracle在存储字符时进行相应的编码映射(查看客户端字符集通过查找注册表中的NLS_LANG键).在客户端查询数 ...
- laravel学习之路5缓存
redis需要先安装 需要通过 Composer 安装 predis/predis 扩展包 (~1.0) 或者使用 PECL 安装 PhpRedis PHP 拓展. composer require ...
- TP 自动验证
#自动验证 一般验证 #自动验证 protected $_validate=array( #函数验证 array('role_name','require','角色名称不能为空!'), array(' ...
- line-height:0的使用
在这里不介绍line-height的概念 建议看一下张鑫旭 写的line-height 文章 这里只说 用到的一些小地方 div中img有间隙的解决方案就是用到了这个小小的属性 那么请看方案 方案 ...
- IP地址、子网掩码、网关的关系
网络管理中的IP地址.子网掩码和网关是每个网管必须要掌握的基础知识,只有掌握它,才能够真正理解TCP/IP协议的设置.以下我们就来深入浅出地讲解什么是子网掩码. IP地址的结构 要想理解什么是子网掩码 ...
- ios 10 sticker pack application
看了WWDC2016直播,我们发现变得谨慎而开放的苹果在新一版四大平台系统中展示了很多变化,当然重中之重还是伟大的iOS.通过试用iOS10beta版,除了长大了的更强大的Siri主要感受到iMess ...
- 【iOS开发-80】Quartz2D绘图简介:直线/圆形/椭圆/方形以及上下文栈管理CGContextSaveGState/CGContextRestoreGState
本文转载至 http://blog.csdn.net/weisubao/article/details/41282457 - (void)drawRect:(CGRect)rect { //获得当前上 ...
- Python小白的发展之路之Python基础(三)【函数简介】
目录: 1. 函数基本语法及特性 2. 参数与局部变量 3. 返回值 4.嵌套函数 5.递归 6.匿名函数 7.函数式编程介绍 8.高阶函数 9.内置函数 1. 函数基本语法及特性 首先我们明确函数是 ...
- SVN流程图协作图
- 关于Spring学习解析顺序
最高接口BeanFactory开始解析,暂定为一级接口! 从上往下进行分析学习! 分析的Spring源码版本是:4.3.3