Python number method choice() returns a random item from a list, tuple, or string.
Following is the syntax for choice() method −
choice( seq )
Note − This function is not accessible directly, so we need to import random module and then we need to call this function using random static object.
seq − This could be a list, tuple, or string.
This method returns a random item.
The following example shows the usage of choice() method.
#!/usr/bin/python import random print "choice([1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]) print "choice('A String') : ", random.choice('A String')
When we run above program, it produces following result −
choice([1, 2, 3, 5, 9]) : 2 choice('A String') : n