Python programming example of a method to generate random usernames and passwords

  • 2020-05-30 20:34:11
  • OfStack

This article demonstrates an example of how Python can be programmed to generate random usernames and passwords. I will share it with you for your reference as follows:

Plan 1:


import random
global userName,userPassword # For ease of use, it is defined as a global variable 
userName = ''
userPassword = ''
def get_userNameAndPassword():
  global userName, userPassword
  usableName_char = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><:}{?/" # A character that can be used as a user name 
  usablePassword_char ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.1234567890" # The characters that can be used as passwords can be added or subtracted as needed 
  e_userName = [] # define 1 A temporary List variable , use list.append Add 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 the username character list
  print"e_userPassword = ", e_userPassword # Output password character list
  userName = ''.join(e_userName)
  userPassword = ''.join(e_userPassword)
try:
  get_userNameAndPassword()
  print " The user name :", userName
  print " password :", userPassword
except Exception, e:
  print e.reason

Program output:


e_userName = ['q', 'M', '2', 'R', 'B', '}', '6', '=']
e_userPassword = ['T', 'O', '4', 'C', 'H', '.']
 The user name : qM2RB}6=
 password : TO4CH.

Option 2 (no intermediate variables) :


#coding=utf-8
import random
global userName,userPassword # It is defined as a global variable for later use 
userName = ''
userPassword = ''
def get_userNameAndPassword():
  global userName, userPassword
  #8 User name and 6 A password 
  userName = ''.join(random.sample("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+=-><:}{?/",8))
  userPassword = ''.join(random.sample("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.1234567890",6))
try:
  get_userNameAndPassword()
  print " The user name :", userName
  print " password :", userPassword
except Exception, e:
  print e.reason

Program output:


 The user name : GweV?2um
 password : fwiOZL

The second method is often used, which is intuitive and simple.

Note: this example runs normally under python2.7.

PS: here are two more online tools for you to use:

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

High strength password generator:
http://tools.ofstack.com/password/CreateStrongPassword

For more information about Python, please check out the topics on this site: Python data structure and algorithm tutorial, Python Socket programming skills summary, Python function skills summary, Python string manipulation skills summary, Python introduction and advanced classic tutorial and Python file and directory manipulation skills summary.

I hope this article has been helpful to you in Python programming.


Related articles: