Note: This site is currently "Under construction". I'm migrating to a new version of my site building software. Lots of things are in a state of disrepair as a result (for example, footnote links aren't working). It's all part of the process of building in public. Most things should still be readable though.

Generate A Random String In Python

Code

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))

Results

1K*fV~X%~[^M
MF(ek#_E\zMiAZ