Python 的 字符串Translate

等级 3105 次按键

这使用str.maketrans()创建的转换表替换字符。

Python 代码

# 创建要转换的字符串
1text = "hello world"
# maketrans创建映射: a->1, e->2, i->3, o->4, u->5
2table = str.maketrans("aeiou", "12345")
# translate应用字符映射
3result = text.translate(table)
# 打印: h2ll4 w4rld(元音替换为数字)
4print(result)

运行结果

h2ll4 w4rld
用这段代码练习打字

可以用这段代码练习打字。成绩不会计入排行榜。

这段代码中的符号

这段 Python 代码中需要输入的符号及其出现次数。

  • "× 6
  • (× 3
  • )× 3
  • =× 3
  • .× 2
  • ,× 1

Python 等级 3 的更多代码示例

← 返回 Python 打字练习