Home > database >  How do I randomly pick ANY character (Letters Symbols Numbers)?
How do I randomly pick ANY character (Letters Symbols Numbers)?

Time:06-17

So I am really new to Python (5 Days). I decided that I wanted to see if I could make a password generator. After around 5 minutes I came up with this code.

import random
import string

length = int(input("Enter the desired length of password: "))
values =string.ascii_letters
for i in range(length):
    letter = random.choice(values)
    print(letter, end="")

This basically asks the user how long they want their password to be and generates a random password of that length. However, the string.ascii_letters thing only allows me to randomize alphabetical letters (a-z)(A-Z). Is there any beginner friendly way in which I can make it choose between all characters like numbers and symbols too. Your advices would be really helpful. Thanks!

CodePudding user response:

Just add them to the values:

values = string.ascii_letters   string.digits   string.punctuation

CodePudding user response:

import string
    string.printable

Would give you all the printable strings

>>> string.printable
 '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()* ,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
  • Related