python random string and number 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 generate random string
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))