python replace two optional strings with a third (looking for a neat way) -


i'd replace 2 optional strings third, i.e. i'd able do:

string.replace(a or b, c) 

yeah yeah can loop on predefined list, or can use 2 replace commands. i'm wondering if there's neater available i'm not aware of.

example

i have string this:

'foo bar baz' 

i'd replace 'foo' 'yay' , 'bar' 'yay' get:

'yay yay baz' 

i can do:

'foo bar baz'.replace('foo', 'yay').replace('bar', 'yay') 

but that's kinda repetitive. i'm thinking there neat way (maybe esoteric function library extends string functions... dunno...)

import re re.sub("a|b", "c", str) 

e.g.

re.sub("foo|bar", "yay", "foo bar baz") 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -