You can use the choice()
method from the random
module:
import random
name = ['Hans', 'Peter', 'Eliza']
print('Your name is ' + random.choice(name) + '!')
random.choice(seq)
Return a random element from the non-empty sequence seq.
If seq is empty, raises IndexError.
Also, I would use str.format()
instead:
import random
name = ['Hans', 'Peter', 'Eliza']
print('Your name is {}!'.format(random.choice(name)))
I missed the part about saving the value. That can be done like this:
name = ['Hans', 'Peter', 'Eliza']
random_name = random.choice(name)
print('Your name is {}!'.format(random_name))