Skip to content

Instantly share code, notes, and snippets.

@TomoG29
Created April 30, 2023 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomoG29/2a51898bfb9f69299f72cee75a7a887d to your computer and use it in GitHub Desktop.
Save TomoG29/2a51898bfb9f69299f72cee75a7a887d to your computer and use it in GitHub Desktop.
str1 = "abcd abcd abcd"
str2 = str1.translate(str1.maketrans({"a": "1", "c": "2"}))
print(str2)
#1b2d 1b2d 1b2d
print(str1.translate(str1.maketrans({"b": "x", "d": None})))
#axc axc axc
print(str1.translate(str1.maketrans({"ab": "xy"})))
# ValueError: string keys in translate table must be of length 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment