The string
module defines a set of characters in various categories.
console
>>> import string
>>> string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_uppercase
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> string.ascii_lowercase
'abcdefghijklmnopqrstuvwxyz'
>>> string.digits
'0123456789'
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> string.whitespace
'\t\n\x0b\x0c\r '
>>> string.printable
'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
Let's use this to generate a password for Tekito.
console
>>> import random, string
#For 4-letter numbers
>>> ''.join([random.choice(string.digits) for i in range(4)])
'7602'
>>> ''.join([random.choice(string.digits) for i in range(4)])
'7531'
#For 8 alphanumeric characters
>>> ''.join([random.choice(string.ascii_letters + string.digits) for i in range(8)])
'84xemCAc'
>>> ''.join([random.choice(string.ascii_letters + string.digits) for i in range(8)])
'cjiGNd2k'
#For 12-character alphanumeric symbols
>>> ''.join([random.choice(string.punctuation + string.ascii_letters + string.digits) for i in range(12)])
'9f58EN+}rbW8'
>>> ''.join([random.choice(string.punctuation + string.ascii_letters + string.digits) for i in range(12)])
'DP4E,N}jtT;W'
You can change the length and type of characters as needed, so you can use it in addition to passwords. maybe? It's annoying to hit the interactive shell every time, so I thought it would be better to use the ʻargparse` module or something to hit from the shell.
Recommended Posts