javanewbie javanewbie - 3 months ago 14
Python Question

Is there a random letter generator with a range?

I was wondering if there is a random letter generator in Python that takes a range as parameter?
For example, if I wanted a range between A and D? I know you can use this as a generator:

import random
import string

random.choice(string.ascii_letters)


But it doesn't allow you to supply it a range.

Answer

You can slice string.ascii_letters:

random.choice(string.ascii_letters[0:4])
Comments