Jul-31-2020, 01:02 PM
(This post was last modified: Jul-31-2020, 01:02 PM by deanhystad.)
Is an example needed? The question is, "Can I use random with replace()?" I take this to mean if we are doing string.replace(old, new) and there are multiple occurrences of "old" in "string", can he somehow use "random" to make a new "new" for each occurrence. I don't see how that can be done.
This obviously doesn't work:
This obviously doesn't work:
import random a = 'Now is the time for all good men to come to the aid of their country' b = a.replace('t', random.choice(('A', 'B', 'C'))) print(b)random.choice gets evaluated and the result is used for "new" in all the replacements.
Output:Now is Ahe Aime for all good men Ao come Ao Ahe aid of Aheir counAry
I cannot think of a way that you can re-evaluate "new" for each replacement. I think you need to look for another solution to this particular problem.