is there a similar function in python that takes search(array) and replace(array) as a parameter? Then takes a value from each array and uses them to do search and replace on subject(string).
I know I can achieve this using for loops, but just looking more elegant way.
The replace() method replace() is a built-in method in Python that replaces all the occurrences of the old character with the new character.
replace() method helps to replace the occurrence of the given old character with the new character or substring. The method contains the parameters like old(a character that you wish to replace), new(a new character you would like to replace with), and count(a number of times you want to replace the character).
replace() Parameters The replace() method can take maximum of 3 parameters: old - old substring you want to replace. new - new substring which will replace the old substring. count (optional) - the number of times you want to replace the old substring with the new substring.
I believe the answer is no.
I would specify your search/replace strings in a list, and the iterate over it:
edits = [(search0, replace0), (search1, replace1), (search2, replace2)] # etc.
for search, replace in edits:
s = s.replace(search, replace)
Even if python did have a str_replace
-style function, I think I would still separate out my search/replace strings as a list, so really this is only taking one extra line of code.
Finally, this is a programming language after all. If it doesn't supply the function you want, you can always define it yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With