Python Forum

Full Version: basic random number generator in replace function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hey, I'm new to python and wanted to know how can I integrate a pseudo-random number generator to a replace function
for example how do I input something like "a) 245 V, b) 736.77 V." and print it with different numbers 3 times
thanks before hand :)
Without an output example, this is not very clear to me.
Paul
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:
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.