Developers · 5 min read

How to generate a random letter in Python

Python has three practical ways to pick a random letter, depending on whether you need general randomness, cryptographic-quality randomness, or weighted results. Here's all three, plus how to restrict output to vowels, consonants, or a custom set.

1. The simple way: random.choice

The standard random module is the right tool for games, tests, and anything that isn't security-sensitive.

import random import string letter = random.choice(string.ascii_uppercase) print(letter)  # e.g. 'Q'

string.ascii_uppercase is just the string "ABCDEFGHIJKLMNOPQRSTUVWXYZ" — swap in string.ascii_lowercase for lowercase, or string.ascii_letters to allow both cases.

2. The secure way: the secrets module

If the letter feeds into anything like a token, password component, or verification code, use secrets instead — it's built for cryptographic use and isn't predictable the way random can be.

import secrets import string letter = secrets.choice(string.ascii_uppercase) print(letter)

3. Restricting to vowels or consonants

Define your own pool and choose from that instead of the full alphabet:

import random vowels = "AEIOU" consonants = "BCDFGHJKLMNPQRSTVWXYZ" random_vowel = random.choice(vowels) random_consonant = random.choice(consonants) print(random_vowel, random_consonant)

4. Generating multiple unique letters

Use random.sample when you need several letters with no repeats — for example, drawing 5 unique letters for a game round:

import random import string letters = random.sample(string.ascii_uppercase, 5) print(letters)  # e.g. ['Q', 'A', 'Z', 'M', 'T']

5. Weighted random letters

To favor common letters (say, matching English letter frequency) over rare ones, use random.choices with weights:

import random letters = list("ABCDEFGHIJKLMNOPQRSTUVWXYZ") weights = [8,1,2,4,12,2,2,6,7,0,1,4,2,6,7,2,0,6,6,9,3,1,2,1,2,1]  # rough English frequency letter = random.choices(letters, weights=weights, k=1)[0] print(letter)

Prefer not to write the code?

The random letter generator on this site does all of the above through a UI — including vowel/consonant filters, excluding specific letters, and generating multiple unique letters at once — with no setup required.