Just for fun, here's a different way to do this immutably, by using a translation map.
If you wanted to replace everything that was in ltrFound
, that would be easy:
tr = str.maketrans(ltrFound, '*' * len(ltrFound))
print(reveal.translate(tr))
But you want to do the opposite, replace everything that's not in ltrFound
. And you don't want to build a translation table of all of the 100K+ characters that aren't s
. So, what can you do?
You can build a table of the 6 characters that aren't in s
but are in reveal
:
notFound = ''.join(set(reveal) - set(ltrFound)) # 'adoprw'
tr = str.maketrans(notFound, '*' * len(notFound))
print(reveal.translate(tr))
The above is using Python 3.x; for 2.x, maketrans
is a function in the string
module rather than a classmethod of the str
class (and there are a few other differences, but they don't matter here). So:
import string
notFound = ''.join(set(reveal) - set(ltrFound)) # 'adoprw'
tr = string.maketrans(notFound, '*' * len(notFound))
print(reveal.translate(tr))