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 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
1import random
2import string
3
4def randStr(chars = string.ascii_uppercase + string.digits, N=10):
5 return ''.join(random.choice(chars) for _ in range(N))
6
7# default length(=10) random string
8print(randStr())
9# random string of length 7
10print(randStr(N=7))
11# random string with characters picked from ascii_lowercase
12print(randStr(chars=string.ascii_lowercase))
13# random string with characters picked from 'abcdef123456'
14print(randStr(chars='abcdef123456'))
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