Home

Generate A Random String In Python

python
import string
import random

def random_string(string_length):
    random_set = string.ascii_letters + string.digits + string.punctuation
    letters = string.ascii_letters
    random_string = ''.join(random.SystemRandom().choice(random_set) for _ in range(string_length))
    return random_string

def random_string_min_max(min, max):
    random_set = string.ascii_letters + string.digits + string.punctuation
    letters = string.ascii_letters
    random_string = ''.join(random.SystemRandom().choice(random_set) for _ in range(random.randint(min,max)))
    return random_string

print(random_string(12))    

print(random_string_min_max(8,20))
TODO: Show Results Output
~ fin ~