描述

Python translate() 方法根据 maketrans() 方法给出的字符映射转换表转换字符串中的字符。

语法

translate() 方法语法:

Python3中:

S.translate(table)

Python2中:

S.translate(table[,delchars])

参数

  • table -- 字符映射转换表表,是通过 maketrans() 方法转换而来的。
  • delchars -- 可选参数,表示要删除的字符组成的字符串。

返回值

返回根据 maketrans() 方法给出的字符映射转换表转换后的字符串,如果给出delchars参数,则会删除这些字符然后进行转换。

实例

以下实例展示了使用 maketrans() 方法加 translate() 方法将所有元音字母转换为指定的数字,并删除指定字符:

Pyhon3中:

#!/usr/bin/python3

intab = "aeiou"
outtab = "12345"
deltab = "thw" trantab1 = str.maketrans(intab,outtab) # 创建字符映射转换表
trantab2 = str.maketrans(intab,outtab,deltab) #创建字符映射转换表,并删除指定字符 test = "this is string example....wow!!!" print(test.translate(trantab1))
print(test.translate(trantab2))

以上实例输出结果如下:

th3s 3s str3ng 2x1mpl2....w4w!!!
3s 3s sr3ng 2x1mpl2....4!!!

Python2中:

#!/usr/bin/python
# -*- coding: UTF-8 -*- import string # 导入string模块 intab = "aeiou"
outtab = "12345"
deltab = "thw" trantab = string.maketrans(intab,outtab) # 创建字符映射转换表 test = "this is string example....wow!!!"; print test.translate(trantab);
print test.translate(trantab,deltab); # Python2中,删除指定字符在 translate() 方法中

以上实例输出结果如下:

th3s 3s str3ng 2x1mpl2....w4w!!!
3s 3s sr3ng 2x1mpl2....4!!!

Python translate()方法的更多相关文章

  1. 《Python CookBook2》 第一章 文本 - 检查字符串中是否包含某字符集合中的字符 && 简化字符串的translate方法的使用

    检查字符串中是否包含某字符集合中的字符  任务: 检查字符串中是否出现了某个字符集合中的字符 解决方案: 方案一: import itertools def containAny(seq,aset): ...

  2. Python 字符串方法详解

    Python 字符串方法详解 本文最初发表于赖勇浩(恋花蝶)的博客(http://blog.csdn.net/lanphaday),如蒙转载,敬请保留全文完整,切勿去除本声明和作者信息.        ...

  3. Python str方法总结

    1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string 1 2 3 4 >>>a = 'shaw' >>> ...

  4. Python String 方法详解

    官网文档地址:https://docs.python.org/3/library/stdtypes.html#string-methods 官网 公号:软测小生ruancexiaosheng 文档里的 ...

  5. Python maketrans() 方法

    描述 Python maketrans() 方法用于给 translate() 方法创建字符映射转换表. 可以只接受一个参数,此时这个参数是个字典类型(暂不研究这种情况). 对于接受两个参数的最简单的 ...

  6. python 字符串方法整理

    Python字符串方法 1.大小写转换 1.1 lower.upper lower():小写 upper():大写 1.2 title.capitalize S.title():字符串中所有单词首字母 ...

  7. Python swapcase()方法

    首先,要明白Python swapcase() 方法用于对字符串的大小写字母进行转换. 其次,了解swapcase()方法语法:str.swapcase() 返回值:返回大小写字母转换后生成的新字符串 ...

  8. python字符串方法的简单使用

    学习python字符串方法的使用,对书中列举的每种方法都做一个试用,将结果记录,方便以后查询. (1) s.capitalize() ;功能:返回字符串的的副本,并将首字母大写.使用如下: >& ...

  9. Python capitalize()方法

    Python capitalize()方法 capitalize()方法返回字符串的一个副本,只有它的第一个字母大写.对于8位的字符串,这个方法与语言环境相关. 语法 以下是capitalize()方 ...

随机推荐

  1. TSL / SSL

    参考: http://www.ruanyifeng.com/blog/2014/09/illustration-ssl.html http://www.tuicool.com/articles/IJ3 ...

  2. dubbo知识点理解2

    作者:网易云链接:https://www.zhihu.com/question/45413135/answer/226794957来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  3. XDroidRequest网络请求框架,新开源

    XDroidRequest 是一款网络请求框架,它的功能也许会适合你.这是本项目的第三版了,前两版由于扩展性问题一直不满意,思考来 思考去还是觉得Google的Volley的扩展性最强,于是借鉴了Vo ...

  4. The maximum number of processes for the user account running is currently , which can cause performance issues. We recommend increasing this to at least 4096.

    [root@localhost ~]# vi /etc/security/limits.conf # /etc/security/limits.conf # #Each line describes ...

  5. DCI:The DCI Architecture: A New Vision of Object-Oriented Programming

    SummaryObject-oriented programming was supposed to unify the perspectives of the programmer and the ...

  6. OpenCV使用二维特征点(Features2D)和单映射(Homography)寻找已知物体

    使用二维特征点(Features2D)和单映射(Homography)寻找已知物体 目标 在本教程中我们将涉及以下内容: 使用函数 findHomography 寻找匹配上的关键点的变换. 使用函数  ...

  7. 如何写科技论文How to write a technical paper

    This is the evolving set of recommendations I share with my graduate students for technical writing. ...

  8. GameObjectPool——Unity中的对象池

    这里介绍一种对象池的写法.它的优点在于无论取出还是插入游戏物体都是常数量时间. using UnityEngine; using System.Collections; using System.Co ...

  9. 在CMD下如何搜索某个名字的文件?

    命令如下: dir FileYouWantToFind.txt /a /s   效果如图:   偷看Kun总操作学来的 ^_^

  10. c# 滚动字幕的实现

    在c#中其实滚动屏幕的实现很简单,只需要用到Graphics.DrawString方法. Graphics.DrawString (String s, Font font, Brush brush, ...