This article describes how to generate a password of a specified length at random in python. it involves the Python character string operation technique and is of great practical value, for more information about how to generate a password of a specified length in python, see the following example. Share it with you for your reference. The details are as follows:
The following python code randomly combines various characters to generate a random password with a specified length
The string object in python has several common methods to output different characters:
string.ascii_letters
All characters that output the ascii code
string.digits
Output '123 '.
string.punctuation
Punctuation in ascii
print string.ascii_lettersprint string.digitsprint string.punctuation
The output result is as follows:
AbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
! "# $ % & '() * +,-./:; <=>? @ [\] ^ _ '{| }~
The following code is used to generate a random password
import stringfrom random import *characters = string.ascii_letters + string.punctuation + string.digitspassword = "".join(choice(characters) for x in range(randint(8, 16)))print password
I hope this article will help you with Python programming.