6

I need to create random word/names with random.choice(alphabet) for many of my games in repl, but it is a pain to type it out, and make uppercase versions, consonants/vowels only, etc.

Is there a built-in or importable way to get a pre-made one in python?

Sumner Evans
  • 8,951
  • 5
  • 30
  • 47
Frasher Gray
  • 222
  • 2
  • 14

1 Answers1

15

The string module provides several (English-centric) values:

>>> import string
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.digits
'0123456789'

You'll have to create your own vowel/consonant lists, as well as lists for other languages.

Given how short the list of vowels is, vowels and consonants aren't too painful:

>>> vowels = set("aeiou")
>>> set(string.ascii_lowercase).difference(vowels)
{'b', 'f', 'v', 'q', 's', 'w', 'y', 'l', 'g', 'j', 'z', 'c', 'h', 'p', 'x', 'd', 'm', 'n', 't', 'k', 'r'}
chepner
  • 497,756
  • 71
  • 530
  • 681
  • 2
    Note that the `set.difference` method accepts any iterable, so `set(string.ascii_lowercase).difference('aeiou')` will do. – blhsing Nov 20 '19 at 18:15