python random string code example
Example 1: python random string
import secrets
secrets.token_hex(nbytes=16)
Example 2: print random string from list python
import random
list = ["Item 1", "Item 2", "Item 3"]
item = random.choice(list)
print(item)
Example 3: 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 4: 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 5: python generate random string
''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(N))
Example 6: 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))