w3resource

Python Math: Get a single random element from a specified string


51. Random Character from String

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

Sample Solution:-

Python Code:

import random
print(random.choice('abcdefghijklm'))

Sample Output:

e 

Pictorial Presentation:

Python Math: Get a single random element from a specified string

Flowchart:

Flowchart: Get a single random element from a specified string

For more Practice: Solve these Related Problems:

  • Write a Python program that takes a user-provided string and uses random.choice() to select and print one random character.
  • Write a Python function to randomly select a vowel from a given string and print the selected vowel along with its index.
  • Write a Python script to pick a random character from a string and display its Unicode code point.
  • Write a Python program that selects a random character from a long paragraph and then determines if it is an alphabetic letter or not.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to generate random even integers in a specific numerical range.
Next: Write a Python program to shuffle the following elements randomly.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.