Computer >> Computer tutorials >  >> Programming >> Python

How to randomly select an item from a string in Python?


The choice() function from random module is helpful for this purpose. It returns one item (a character in case of a string) randomly from string (any sequence object for that matter)

>>> import random
>>> string='TutorialsPoint'
>>> ch=random.choice(string)
>>> ch
'n'
>>> ch=random.choice(string)
>>> ch
'u'