lang/python/ StringTranslation


Like *nix's {{tr}} (in Python 3) {{c "Hello World".translate(str.maketrans("lo","XY"))

returns: "HeXXY WYrXd"

}}

Stuff that *nix's {{tr}} can't do: {{c a = "Hello World" d = {ord('l'):'Boing',ord('o'):'Snarf' a.translate(d)

returns: "HeBoingBoingSnarf WSnarfrBoingd"

}}}

To see what {{str.maketrans}} does: {{c str.maketrans("lo","XY")

returns: {108: 88, 111: 89}

}} the //keys// need to be integer character codes. You go from character code to string via {{chr()}} and from character to character code via {{ord()}}. The //values// can be either integers (character codes), or strings, or None if you want to delete characters. %TIME=1629220889