Python programming to generate a random user name and password example, python example
This article describes how to generate a random user name and password through Python programming. We will share this with you for your reference. The details are as follows:
Solution 1:
Import randomglobal userName, userPassword # For ease of use, it is defined as the global variable userName = ''userpassword = ''def get_userNameAndPassword (): global userName, userPassword usableName_char =" 1234567890 bytes! @ # $ % ^ & * () _ + =-> <:}{? /"# UsablePassword_char =" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _. 1234567890 "# can be used as a password character. You can add or remove e_userName = [] # as needed to define a temporary List variable and use list. append Add the character e_userPassword = [] for I in range (8): e_userName.append (random. choice (usableName_char) for j in range (6): e_userPassword.append (random. choice (usablePassword_char) print "e_userName =", e_userName # output userName Character list print "e_userPassword =", e_userPassword # output Password character list userName = ''. join (e_userName) userPassword = ''. join (e_userPassword) try: get_userNameAndPassword () print "userName:", userName print "Password:", userPasswordexcept Exception, e: print e. reason
Program output:
E_userName = ['Q', 'M', '2', 'R', 'B', '}', '6 ', '='] e_userPassword = ['T', 'O', '4', 'C', 'h ','. '] User name: qM2RB} 6 = password: TO4CH.
Solution 2 (excluding intermediate variables ):
# Coding = utf-8import randomglobal userName, userPassword # to facilitate later use, defined as the global variable userName = ''userPassword ='' def get_userNameAndPassword (): global userName, userPassword #8-bit userName and 6-bit password userName = ''. join (random. sample ("1234567890 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ! @ # $ % ^ & * () _ + =-> <:}{? /", 8) userPassword = ''. join (random. sample ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ _. 1234567890 ", 6) try: get_userNameAndPassword () print" userName: ", userName print" Password: ", userPasswordexcept Exception, e: print e. reason
Program output:
Username: GweV? 2um password: fwiOZL
The second method is commonly used, which is intuitive and simple.
Note: (In this example, the test runs normally under python2.7 .)
PS: Here are two related online tools for your reference:
Online random number/string generation tool:
Http://tools.jb51.net/aideddesign/suijishu
High-strength Password generator:
Http://tools.jb51.net/password/CreateStrongPassword