0
How do you print a random number or string from a list of integers and strings in python?
2 Respostas
+ 3
You should show your attempt first. But here some hints:
- use module random
- use function randint() from random and set start value to 0 and end value to length of list
- use this ramdom value as an index to access the list (remember: index starts at 0)
- use print() function to output result
+ 2
Hi Amitoj, you can do it by importing the random module.
You can use random.choice() method.
Example:
import random
number_list = [4, 12, 45, 56, 78,32]
str_list = ["Python", "Java", "Kotlin"]
rand_num = random.choice(number_list)
rand_str = random.choice(str_list)
print("Random Number : ",rand_num)
print("Random String : ",rand_str)
Sample Output:
Random Number : 45
Random String : Kotlin
Note: if the above program is executed a random number and a random string will be displayed as output.