Python random choice() Method
The choice() method returns a list, tuple or string of random items.
Python random choice() Syntax
Here is the syntax of the choice() method:
import random
random.choice( seq )
Note: choice() is not directly accessible, you need to import the random module and then call the method via the random static object.
Python random choice() Parameters
- seq : can be a list, a tuple or a string.
Python random choice() Return Value
Return the random item.
Python random choice() Example#1
The following shows an example of using the choice() method.
#!/usr/bin/python3
import random
print ("Return a random number from range(100) : ",random.choice(range(100)))
print ("Return a random element from the list [1, 2, 3, 5, 9]) : ", random.choice([1, 2, 3, 5, 9]))
print ("Return a random character from the string 'Apidemos' : ", random.choice('Apidemos'))
Output: