1import random
2import string
3
4def random_string_generator(str_size, allowed_chars):
5 return ''.join(random.choice(allowed_chars) for x in range(str_size))
6
7chars = string.ascii_letters + string.punctuation
8size = 12
9
10print(chars)
11print('Random String of length 12 =', random_string_generator(size, chars))
1import random
2
3list = ["Item 1", "Item 2", "Item 3"] # List
4item = random.choice(list) # Chooses from list
5print(item) # Prints choice
6
7# From stackoverflow
8# Tried and tested method
1import secrets
2secrets.token_hex(nbytes=16)
3
4# this will produce something like
5# aa82d48e5bff564f3221d02194611c13
1import string
2import random
3
4length=5
5#python2
6randomstr = ''.join(random.sample(string.ascii_letters+string.digits,length))
7
8
9#python3
10randomstr = ''.join(random.choices(string.ascii_letters+string.digits,k=length))
11
12
1# -random letter generator-
2import string
3var1 = string.ascii_letters
4
5import random
6var2 = random.choice(string.ascii_letters)
7print(var2)
1''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
2