How to generate a random string with symbols

computing245 picture computing245 · Nov 2, 2017 · Viewed 14.5k times · Source

My code:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits ) for n in range(12)])

So far it prints a string which contains uppercase, lowercase letters and numbers, but I don't know how to make it print symbols as well.

Answer

Luke Smith picture Luke Smith · Nov 2, 2017

How about:

import random
import string
random = ''.join([random.choice(string.ascii_letters + string.digits + string.punctuation ) for n in range(12)])