create a password with a custom length and symbols in python

You can create a password with a custom length and symbols in Python by using the random module to generate random characters and symbols. Here's a code snippet that creates a password with a length of 10 characters and includes symbols:

main.py
import random
import string

# define the length of the password
length = 10

# define the characters and symbols to use
characters = string.ascii_lowercase + string.ascii_uppercase + string.digits
symbols = "!@#$%^&*()_+-=[]{};:,.<>/?"
all_characters = characters + symbols

# generate a password using a random selection of characters and symbols
password = ''.join(random.choice(all_characters) for _ in range(length))

# print the password
print("Your password is:", password)
481 chars
17 lines

In this code, the string module is used to define the characters to use (lowercase letters, uppercase letters, and digits) as well as a set of symbols. The all_characters variable combines both sets of characters and symbols. Then, the random module is used to generate a password with a length of length by selecting a random character from all_characters for each of the length characters in the password. The join() function combines the characters into a string, which is then printed to the console.

gistlibby LogSnag