I want to replace every character in a string with a '-' except for the entered variable.
For example, if guessedLetter = h, the string 'house' would become 'h----'
I want to replace every character in a string with a '-' except for the entered variable.
For example, if guessedLetter = h, the string 'house' would become 'h----'
You can use list comprehension
guessedLetter = 'h'
string = 'house'
new = ''.join(['-' if let != guessedLetter else guessedLetter for let in string])
output
h----
You need regex
:
import re
guessedLetter = 'o'
s = 'house'
result = re.sub(f'[^{guessedLetter}]', '-', s)