generate random string python code example
Example 1: python random string
import secrets
secrets.token_hex(nbytes=16)
Example 2: generate random string python
import string
import random
length=5
randomstr = ''.join(random.sample(string.ascii_letters+string.digits,length))
randomstr = ''.join(random.choices(string.ascii_letters+string.digits,k=length))
Example 3: how to make a module that generates a random letter in python
import string
var1 = string.ascii_letters
import random
var2 = random.choice(string.ascii_letters)
print(var2)
Example 4: python generate random string with lenght
import random
import string
def randStr(chars = string.ascii_uppercase + string.digits, N=10):
return ''.join(random.choice(chars) for _ in range(N))
print(randStr())
print(randStr(N=7))
print(randStr(chars=string.ascii_lowercase))
print(randStr(chars='abcdef123456'))
Example 5: python random string
import random
import string
def random_string_generator(str_size, allowed_chars):
return ''.join(random.choice(allowed_chars) for x in range(str_size))
chars = string.ascii_letters + string.punctuation
size = 12
print(chars)
print('Random String of length 12 =', random_string_generator(size, chars))
Example 6: generate random string python
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))