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 ...
随机推荐
- redhat安装中文man手册
1.下载中文man手册 http://download.chinaunix.net/download.php?id=13232&ResourceID=6537 2.上传至服务器并解压 tar ...
- MS SQL 分类汇总参数 grouping(**)=1 rollup cubt
转:http://www.111cn.net/database/mssqlserver/43368.htm 本文章介绍了关于sql多级分类汇总实现方法及数据结构,有碰到问题的同学可参考一下. 据库结构 ...
- Unity Editor Inspector编辑模板
效果图: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEdito ...
- 不使用库函数,编写函数int strcmp(char *source, char *dest) 相等返回0,不等返回-1;
答案:一. int strcmp(char *source, char *dest) { /* assert的作用是现计算表达式 expression ,如果其值为假(即为0),那么它先向stder ...
- ULN2003A 使用,有坑
8脚接24V负极 9脚接24V正极 16接24V继电器,再接到24V正极 1-7无论给5V 正 或 负,10-16都不能达到24V,越靠近输入端的输出端电压越大,最大的才11V,最小的2.5V 最后发 ...
- 安装部署服务器和javaweb项目
[说明]总算告一段落了,服务器啊服务器,你可是把我折磨的够呛,不过现在的状态我已经很满足了. [说明]下面的图片是我这两天一直在搞的,内容不能说是重复,只能说是不停地修改修改,出错出错. 1) 虚拟主 ...
- task19-21
[说明]理想是丰满的,现实很骨感,昨天还说今天有望干掉5个小任务,看来是没可能了,兜兜转转地做了一天也才完成下面的这些 一:今日完成 19.学习Spring,配置Spring和Junit 1)先安装一 ...
- iOS 字符串截取,将字符串中用括号包含的内容去除
//去除字符串中用括号括住的位置 -(NSString *)handleStringWithString:(NSString *)str{ NSMutableString * muStr = [NSM ...
- 5.Django数据库配置
Django默认支持sqlite.mysql.oracle.postgresql数据库,像db2和sqlserver需要安装第三方的支持 配置Django数据库:\hello_django\hello ...
- 如何用excel urldecode解码把url编码转为汉字?
统计分析可以反映出网站运营的情况,并根据实际作出相应的调整,是站长必需的基础技能.ytkah感觉最好用的是谷歌统计,里面有个搜索关键词及对应受访页面,这个功能对优化用处很大,但大家都知道访问不太顺畅. ...