3

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)

1 Answer 1

7

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))
Sign up to request clarification or add additional context in comments.

1 Comment

@Hulk, as the docs at docs.python.org/library/random.html#random.randrange say, randrange was new in version 1.5.2. No idea why you think you can't use it 8 versions later than than time?!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.