About Lesson
choice() Method
The choice() method returns a randomly selected element from the specified sequence.
The sequence can be a string, a range, a list, a tuple or any other kind of sequence.
Syntax
random.choice(sequence)
Parameter Values
Parameter |
Description |
sequence |
Required. A sequence like a list, a tuple, a range of numbers etc. |
Example
Return a random element from a list:
import random
myl = [“apple”, “banana”, “cherry”]
print(random.choice(myl))
Output:
Apple
Example
Return a random character from a string:
import random
x = “WELCOME”
print(random.choice(x))
Output:
C