Generate a random letter in Python

Generate a random letter in Python

Simple:

>>> import string
>>> string.ascii_letters
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
>>> import random
>>> random.choice(string.ascii_letters)
j

string.ascii_letters returns a string containing the lower case and upper case letters according to the current locale.

random.choice returns a single, random element from a sequence.

>>> import random
>>> import string
>>> random.choice(string.ascii_letters)
g

Generate a random letter in Python

>>>def random_char(y):
       return .join(random.choice(string.ascii_letters) for x in range(y))

>>>print (random_char(5))
>>>fxkea

to generate y number of random characters

Leave a Reply

Your email address will not be published. Required fields are marked *