Python String translate() function returns a new string with each character in the string replaced using the given translation table.

Python字符串translate()函数返回一个新字符串,该字符串中的每个字符都使用给定的转换表替换。

Python字符串translate() (Python String translate())

The translation table must be a mapping of Unicode ordinals to Unicode ordinals, strings, or None.

转换表必须是Unicode常规到Unicode常规,字符串或无的映射。

We can create a translation table using maketrans() function or provide it manually using a dictionary mapping.

我们可以使用maketrans()函数创建翻译表,也可以使用字典映射手动提供它。

We can pass maximum three string arguments to maketrans() function.

我们最多可以将三个字符串参数传递给maketrans()函数。

  • If there is only one argument, it must be a dictionary mapping Unicode ordinals (integers) or characters to Unicode ordinals, strings or None.如果只有一个参数,则它必须是将Unicode序数(整数)或字符映射到Unicode序数,字符串或无的字典。
  • 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.如果有两个参数,则它们必须是长度相等的字符串,并且在结果字典中,x中的每个字符都将映射到y中相同位置的字符。
  • If there is a third argument, it must be a string, whose characters will be mapped to None in the result.如果有第三个参数,则它必须是一个字符串,其字符将在结果中映射为None。

Python字符串translate()示例 (Python String translate() Examples)

Let’s look at some examples of using string translate() function.

让我们看一些使用字符串translate()函数的示例。

带有一个参数的maketrans() (maketrans() with one argument)

s = 'ABCDBCA'translation = s.maketrans({ord('A'): 'a', ord('B'): ord('b')})  # single argument as dict
print(s.translate(translation))

Output: abCDbCa

输出: abCDbCa

Here ‘A’ is being replaced with ‘a’ and ‘B is being replaced with ‘b’ in the result string.

结果字符串中的“ A”被替换为“ a”,而“ B”被替换为“ b”。

maketrans()有两个参数 (maketrans() with two arguments)

s = 'ABCDBCA'translation = s.maketrans('A', 'a')print(s.translate(translation))translation = s.maketrans('ABCD', 'abcd')
print(s.translate(translation))

Output:

输出:

aBCDBCa
abcdbca

The first translation is replacing ‘A’ with ‘a’.

第一个翻译是将“ A”替换为“ a”。

The second translation has two string arguments of the same length and each character in the first string is being mapped with the corresponding index character in the second string. So A will be replaced with a, B will be replaced with b, C will be replaced with c and D will be replaced with d in the result string.

第二个转换具有两个相同长度的字符串参数,并且第一个字符串中的每个字符都与第二个字符串中的相应索引字符进行映射。 所以A将被替换aB将与被替换bC将与被替换cD将被替换d在结果字符串。

If the two argument strings are of different length, then an error will be raised.

如果两个参数字符串的长度不同,则会引发错误。

translation = s.maketrans('AB', 'a')

Error: ValueError: the first two maketrans arguments must have equal length

错误: ValueError:前两个maketrans参数的长度必须相等

带有三个参数的maketrans() (maketrans() with three arguments)

s = 'ABCDBCA'translation = s.maketrans('AB', 'ab', 'ACD')
print(s.translate(translation))

Output: bb

输出: bb

Here ‘A’ is first being replaced by ‘a’ but then overridden to None because of third-string argument. Then ‘B’ is mapped with ‘b’. ‘C’ and ‘D’ characters are mapped to None for translation.

在这里,“ A”首先被“ a”替换,但是由于第三个字符串参数而被覆盖为None。 然后,“ B”被映射为“ b”。 “ C”和“ D”字符映射为“无”以进行翻译。

If we provide more than three arguments, then an error is raised.

如果我们提供三个以上的参数,则会引发错误。

translation = s.maketrans('AB', 'ab', 'CD', 'c')

Error: TypeError: maketrans() takes at most 3 arguments (4 given)

错误: TypeError: maketrans() takes at most 3 arguments (4 given)

带有手动映射的Python字符串translate() (Python String translate() with manual mapping)

s = 'ABCDBCA'print(s.translate({ord('A'): ord('a'), ord('B'): ord('b'), ord('C'): None}))
print(s.translate({ord('A'): 'X', ord('B'): 'YZ', ord('C'): None}))

Output:

输出:

abDba
XYZDYZX

Notice that in the second statement ‘B’ is being replaced by the string ‘YZ’. Other mappings are a simple character to character replacement. I am using ord() function to provide the Unicode code point for the translation mappings.

请注意,在第二个语句中,“ B”被字符串“ YZ”替换。 其他映射是简单的字符到字符替换。 我正在使用ord()函数为翻译映射提供Unicode代码点。

GitHub Repository.GitHub存储库中检出完整的python脚本和更多Python示例。

Reference: Official Documentation

参考: 官方文档

翻译自: https://www.journaldev.com/23697/python-string-translate

Python字符串translate()相关推荐

  1. python 字符串函数_Python字符串函数

    python 字符串函数 Python provides a lot of built-in functions to manipulate strings. Python String is imm ...

  2. python字符串lower,upper,capwordds方法,translate(使用替换后),maketrans(创建表)的使用(不知道怎么回事这个变乱了?后期再整理)

    python字符串lower,upper,capwordds方法的使用 print("Skyline".lower()) # 转为小写skyline print("Sky ...

  3. Python 字符串方法详解

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

  4. python字符串之查找与替换_Python字符串操作(查找,替换,分割和连接)方法及其使用...

    str 提供了如下常用的执行查找.替换等操作的方法: startswith():判断字符串是否以指定子串开头. endswith():判断字符串是否以指定子串结尾. find():查找指定子串在字符串 ...

  5. 2.1.Python字符串处理(去掉空格或者特殊字符、替换操作、查找操作、判断操作、分割合并操作、字符串文档)

    2.1.Python字符串处理 2.1.1.去掉空格或者特殊字符 2.1.2.替换操作 2.1.3.查找操作 2.1.4.判断操作 2.1.5.分割合并操作 2.1.6.字符串文档 2.1.Pytho ...

  6. 7.python字符串-内置方法分析

    上篇对python中的字符串内置方法进行了列举和简单说明,但这些方法太多,逐一背下效率实在太低,下面我来对这些方法按照其功能进行总结: 1.字母大小写相关(中文无效) 1.1 S.upper() -& ...

  7. python字符串函数运算_Python入门教程2. 字符串基本操作【运算、格式化输出、常用函数】 原创...

    前面简单介绍了python基本运算,这里再来简单讲述一下Python字符串相关操作 1. 字符串表示方法 >>> "www.jb51.net" #字符串使用单引号 ...

  8. python字符串的表示_Python字符串方法总结

    Python字符串方法图示: (温馨提示:对图片点右键--在新标签页中打开图片) 1.index() 定义:查找并返回指定str的索引位置,如果没找到则会抛异常(查找的顺序是从左至右)可以指定范围:开 ...

  9. python string 方法,python字符串的方法与操作大全

    一:字符串的方法与操作 *注意:首字母为l的为从左边操作,为r的方法为从右边操作 1.__contains__()判断是否包含 判断指定字符或字符串是否包含在一个字符串内,返回值为true或者fals ...

最新文章

  1. HDU2544(SPFA算法)
  2. ecshop文章增加点击次数插件
  3. 万字长文:详解 Spring Boot 中操作 ElasticSearch
  4. 7月第4周全球域名商(国际域名)新增注册量TOP15
  5. 在OpenWrt中上传文件至路由器
  6. 论文笔记:CycleGAN
  7. python丢失api-ms-win-crt-process_api-ms-win-crt-process-l1-1-0.dll 丢失的处理,遇到问题和完美解决...
  8. JSP中的include指令
  9. Linux,没你想象的那么安全!
  10. 1.1 线性模型基础
  11. java 获取及修改系统变量
  12. ByteBuf详解和Netty中的拆包粘包原理解析
  13. java微信提现_如何做提现到微信和支付宝
  14. 关于LANP的相关常识题
  15. ATFX:美国7月CPI前瞻,及美元指数走势判断
  16. 七日杀服务器直连教程,七日杀IP直连的方法
  17. 基于IMS的VoLTE业务
  18. tablueau地图标记圆形_多点钉图标记-简单易用的地图位置标记标注工具
  19. 【栈】实现高级计算器
  20. 转载_纯Java代码批量去除图片文字水印

热门文章

  1. ios 将随意对象存进数据库
  2. [转载] python关键字和保留字_Python关键字
  3. [转载] python 判断字符串是否包含另一个字符串_强烈推荐:Python字符串(string)方法整理(一)...
  4. Eclipse------新建文件时没有JSP File解决方法
  5. bzoj千题计划269:bzoj2655: calc (拉格朗日插值)
  6. 数据结构上机实践第14周项目1(4) - 验证算法(平衡二叉树)
  7. 数据结构上机实践第11周项目2 - 操作用邻接表存储的图
  8. tensorflow随笔——图像分类、检测,语义分割综述
  9. python怎么读数据库_Python如何读写SQLite数据库
  10. 编译原理第三版王生原pdf_CS143:编译原理 | 环境搭建HelloWorld