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

Changing a character globably

So im trying out a some stuff in python, like math, quizzes, simple calculator and other stuff, ive been donig this for about a 3 weeks and have completed them mostly without help but a simple thing i wanted to try i found out to be much harder than i thought, and it is simply how to change a charactor universily, like when it prints a t it will always be a i, i would like to know if this is possible and how to do it, looking forward to a response.
i also searched alot on the internett but there dosent seem to be an answer anywhere, maybe this thread will help others too.

I tried name.replace("bla bla", "blah blah"
it does work but dosent chane every bla bla to blah blah

note: this is just for testing, and was just wondering if it was possible
and im sorry for beinga little unclear, the thing i was wondering was if it was able to change a charactor to always be another like every time i for example print("Hello") that no matter what it will always come out in the form of Helli because the o is replaced

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 :

If I understood you correctly – the str.replace() method is indeed the right tool for the job, but it seems like you might not be using it correctly or there’s a misunderstanding in its behavior.

The str.replace(old, new[, count]) method returns a copy of the string with all occurrences of the substring old replaced by new. If the optional argument count is given, only the first count occurrences are replaced.

Here’s how you would typically use it to replace all occurrences of the character ‘t’ with ‘i’:

text = "this is a test"
new_text = text.replace('t', 'i')
print(new_text)  # "ihis is a iesi"

This will replace every ‘t’ in the string with an ‘i’.

However, if you want this to happen universally — meaning, anytime you print something, a ‘t’ would automatically be replaced with an ‘i’ without you having to call replace every time — you would need to write a custom print function that does this replacement for you before printing:

def custom_print(text):
    text = text.replace('t', 'i')
    print(text)

# Now when you use custom_print, it will replace 't' with 'i'
custom_print("this is a test")  # "ihis is a iesi"

If replace isn’t working as expected, make sure that you are using the return value of replace, since strings in Python are immutable and replace does not change the original string but returns a new one.

Good luck!

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