How to generate a random word or letter in Python


This post goes over how to generate a random word or letter in Python.

Random Word

Install

Install random-word and PyYaml:

pip3 install random-word pyyaml

PyYaml is required or else you’ll get the error:

ModuleNotFoundError: No module named 'yaml'

Usage

Generate a random word:

from random_word import RandomWords

random_words = RandomWords()
print(random_words.get_random_word())

See the package documentation for more information.

Demo

Replit:

Random Letter

Get a random letter from the alphabet:

from string import ascii_lowercase
from random import randrange

print(ascii_lowercase[randrange(len(ascii_lowercase))])

Demo

Replit:



Please support this site and join our Discord!