Search code examples
pythonrandomappendlettergenerate

Randomly Generate Letters in python


I want to generate 10 unique English letters and append them to a list in Python.

This is what I tried:

for i in range(10):
    rand = random.choice(string.ascii_letters)
    print(rand)

Solution

  • This can be also the answer:

    import random
    import string
    
    ls = []
    while len(ls) !=10:
        rand = random.choice(string.ascii_letters)
        rand =rand.lower()
        if rand not in ls:
            ls.append(rand)
    

    As this does get repeated alphabets. This code gets a random Unique 10 letters