Skip to content
Home » random.choice() Python

random.choice() Python

The random.choice() function is used to select an item at random from a specified list, tuple, or string. The Below example helps you to understand better.

Syntax:

random.choice(input_sequence)

# random choice() method
import random

# returns random element from the list
list1 = [5,7,2,9,1,0]
print(random.choice(list1))

# returns random character from the string
string = "similargeeks"
print(random.choice(string))

# returns random item from the tuple
tuple2 = (5,7,2,8,9,1)
print(random.choice(tuple2))

Output:

0
e
7

Also Read:

Generate List of random numbers using Loop : Python