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

PythonServer Side ProgrammingProgramming

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'
raja
Published on 05-Mar-2018 07:01:47
Advertisements