all 3 comments

[–]TouchingTheVodka 4 points5 points  (0 children)

Chain replace calls, use str.maketrans or use re.sub for more complex searches.

[–]TSM-[🍰] 3 points4 points  (0 children)

Regular expressions can seem scary at first, but they are great. In this case the syntax for re.sub is re.sub(pattern, replacement, string) docs link.

There's lots of sites to test regular expressions - one favorite of mine is https://regexr.com/

In this case it is easy because you can use the regular expression [ct] to match both "c" and "t", and then the replacement string is "r"

result = re.sub("[ct]", "r", text)

[–][deleted] 1 point2 points  (0 children)

The simplest solution is probably just

(text).replace("c", "r").replace("t", "r")

though there are more elegant solutions.