使用字符映射翻译字符串

Translate a string using a character map

考虑以下模式

A​​→D
M → P
X → A
→ d
m → p
x → 一个

我需要编写一个程序来解决以下消息

Vrphwklqjphdqlqjixo

如果python中有任何内置函数,请告诉我。

提前致谢。

您似乎要查找的内置函数是 str.translate:

S.translate(table [,deletechars]) -> string

Return a copy of the string S, where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256 or None.
If the table argument is None, no translation is applied and the operation simply removes the characters in deletechars.

或者,如果您使用旋转方案,您可能会发现 chrord 函数很有用:

chr(i) -> character

Return a string of one character with ordinal i; 0 <= i < 256.

ord(c) -> integer

Return the integer ordinal of a one-character string.