Hulk
Hulk

Reputation: 34200

Constructing a random string

How to construct a string to have more than 5 characters and maximum of 15 characters using random function in python

    import string

    letters = list(string.lowercase)

Upvotes: 3

Views: 283

Answers (1)

Alex Martelli
Alex Martelli

Reputation: 882741

After the import and assignment you already have, assuming you want all possible lengths with the same probability:

import random

length = random.randrange(5, 16)

randstr = ''.join(random.choice(letters) for _ in range(length))

Upvotes: 7

Related Questions