I would like to create a variable which contains a name with 3-7 random letters. (It doesn't matter if the name is incorrect). I know how to do it to contain 3 letters but I just don't have a clue how to handle a range with letters (string/list). This is my code so far:
import random
ABC = ['A', 'Á', 'B', 'C'... just imagine every single letter of the Hungarian alphabet as a list]
name = random.sample(ABC, 3)
I tried to do it like this:
name = random.sample(ABC, 3) or random.sample(ABC, 4)
but it didn't work, of course. Can you guys help me out, please? :)
random.sample(ABC, 3) returns a list. As follows
>>> random.sample(ABC, 3)
['q', 'h', 'x']
you can convert it to string using join method
>>> a = random.sample(ABC, 3)
>>> a
['q', 'h', 'x']
>>>''.join(a)
'qhx'
Edit: You can even choose the number in between 3 and 7 by using randint of random module
>>> a = random.sample(ABC, random.randint(3,7))
>>> ''.join(a)
'bvt'
>>> b = random.sample(ABC, random.randint(3,7))
>>> ''.join(b)
'fycu'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With