You can do this, however, you'll have to specify the maximum length that the string can be. You could always generate a random number for max_length
as well.
import random, string
def random_string(length):
return ''.join(random.choice(string.lowercase) for i in range(length))
max_length = 5
print(random_string(random.randint(1, max_length)))
Here are 5 test cases:
print(random_string(random.randint(1, max_length)))
print(random_string(random.randint(1, max_length)))
print(random_string(random.randint(1, max_length)))
print(random_string(random.randint(1, max_length)))
print(random_string(random.randint(1, max_length)))
And their output is:
wl
crhxl
uvl
ty
iuydl
You can of course change the maximum length for the string by changing the max_length
variable to whatever you'd like.