I need my program to replace a certain "-" with a variable. I’ve tried this but it replaces the whole thing with the varibale.
jelenleg = 8 * "-"
x = 0
guess = c
jelenleg = jelenleg[x].replace("-", guess)
print(jelenleg)
So I need this to happen:
before: ——–
after: c——-
But instead what I get is this: c
>Solution :
You can specify the count of to be replaced items:
jelenleg.replace("-", guess, 1)
will only replace one -
To replace at a particular location, I cant think of anything easier than transforming the string into a list, then replacing, then back to a string, like this:
jelenleg_list = list(jelenleg)
jelenleg_list[x] = guess
jelenleg = "".join(jelenleg_list)