create a password with a custom length in python

To create a password with a custom length in Python, you can use the string and random modules. The string module provides a list of printable characters (letters, digits, punctuation, etc.), while the random module contains functions to generate random numbers or selections.

Here's an example code snippet that creates a password with a length of 10 characters:

main.py
import string
import random

length = 10
password = ''.join(random.choice(string.printable) for i in range(length))
print(password)
132 chars
7 lines

In this code, string.printable is a string that contains all the printable characters, and random.choice(string.printable) selects a random character from it. The join() method concatenates the selected characters into a single string with a length of length. Finally, the resulting password is printed to the console.

You can adjust the value of length to generate a password with a different number of characters.

gistlibby LogSnag