try the following:
def getCode(length = 10, char = string.ascii_uppercase + string.digits + string.ascii_lowercase ): return ''.join(random.choice( char) for x in range(length))
mileage:
>>> import random >>> import string >>> getCode() '1RZLCRBBm5' >>> getCode(5, "mychars") 'ahssh'
if you have a list, you can do it like this:
>>> set_list = ['a','b','c','d'] >>> getCode(2, ''.join(set_list)) 'da'
if you want to use special characters, you can use string punctuation:
>>> print string.punctuation !"#$%&'()*+,-./:;<=> ?@ [\]^_`{|}~
source share