Is there a simple, straightforward way to turn this string:
"aBCd3Fg"
Into:
"a**d3*g"
In python 2.7?
Not sure how fast you need this, but if you're looking for the fastest solution out there. The python string module's translate function is a slightly more roundabout, though generally more performant method:
import string
transtab = string.maketrans(string.uppercase, '*'*len(string.uppercase))
"aBCd3Fg".translate(transtab)
>>>'a**d3*g'
I'm always surprised about how many people don't know about this trick. One of the best guarded secrets in python IMO
import re
print re.sub(r'[A-Z]', '*', "aBCd3Fg")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With