Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

.replace function now working as intented (python)

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading