I’m trying to use the string.replace() with a list of specified things that I wish to find and replace:
string = 'Hello %*& World'
string.replace(['%','*','&'], ['pct','star','and'])
I’m sure a dictionary would make a lot more sense, but the example was just for you guys to try to understand what I’m trying to do. Any help would be greatly appreciated!
I’m using Python 3.
Just use
str.replacemultiple times…However, such a way of storing replacements doesn’t look good. One possibility is to use a dictionary:
Also, if you know that the strings that are to be replaced are always just one character,
str.translatecan be more efficient. Use it like this: