Python programming to generate a random user name and password example, python example

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.