Write a Python program to get a single random element from a specified string


The program uses the random module in Python to select a random character from the string "Python Exercise" and print it to the console.

  • random.choice(str) selects a random character from the string str.
  • The string "Python Exercise" is assigned to the variable str.
  • print(random.choice(str)) prints the randomly selected character to the console.

So, each time the program runs, a random character from the string "Python Exercise" is selected and printed to the console. The output will be a single character, such as "h" or "x", chosen at random. Note that since the characters are chosen randomly, the output will be different each time you run the program.

Source Code

import random
str = "Python Exercise"
print(random.choice(str))

Output

s

Example Programs