SoFunction
Updated on 2024-11-14

Python randomly generated 8-digit password example details

Code example 1 (8 bits)

import random
import string


total = string.ascii_letters +  + 


length = 8


password = "".join((total, length))

print(password)

Code example 2 (arbitrary)

import random
import math


alpha = "abcdefghijklmnopqrstuvwxyz"
num = "0123456789"
special = "@#$%&*"


# Enter password length
pass_len = int(input("Enter password length"))


# Set the length of the password composition format, the proportion of alphanumeric and special symbols, the following example 50-30-20 ratio allocation
alpha_len = pass_len//2
num_len = (pass_len*30/100)
special_len = pass_len-(alpha_len+num_len)


password = []


def generate_pass(length, array, is_alpha=False):
       for i in range(length):
        index = (0, len(array) - 1)
        character = array[index]
        if is_alpha:
            case = (0, 1)
            if case == 1:
                character = ()
        (character)


# Characters
generate_pass(alpha_len, alpha, True)
# Number
generate_pass(num_len, num)
# Special symbols
generate_pass(special_len, special)
# Reverse the order
(password)
# To list to string
gen_password = ""
for i in password:
    gen_password = gen_password + str(i)
print(gen_password)

Knowledge supplementation

Python implementation for generating random alphabet + number passwords

#coding:utf-8
import random,string
def GetPassword(length):
  # of randomly generated numbers
  Ofnum=(1,length)
  Ofletter=length-Ofnum
  # Selected ofnum numbers
  slcNum=[() for i in range(Ofnum)]
  # Check the letter ofletter
  slcLetter=[(string.ascii_letters) for i in range(Ofletter)]
  # Disrupt the mix
  slcChar=slcLetter+slcNum
  (slcChar)
  # Generate random passwords
  getPwd=''.join([i for i in slcChar])
  return getPwd
if __name__=='__main__':
  print( GetPassword(6)) #GetPassword()Customize random password length

Python randomly generates passwords with special characters

#!/usr/bin/env python
import string
from itertools import chain
from random import choice, sample
def mkpasswd(length=12, digits=4, upper=3, lower=3):
lowercase = 
uppercase = 
salt = '!@#$%^&*()><?'
password = list(
chain(
(choice(uppercase) for _ in range(upper)),
(choice(lowercase) for _ in range(lower)),
(choice() for _ in range(digits)),
(choice(salt) for _ in range((length - digits - upper - lower)))
)
)
return "".join(sample(password, len(password)))
if __name__ == '__main__':
print mkpasswd()
#!/usr/bin/env python
import string
from itertools import chain
from random import choice, sample
def mkpasswd(length=12, digits=4, upper=3, lower=3):
lowercase = 
uppercase = 
salt = '!@#$%^&*()><?'
password = list(
chain(
(choice(uppercase) for _ in range(upper)),
(choice(lowercase) for _ in range(lower)),
(choice() for _ in range(digits)),
(choice(salt) for _ in range((length - digits - upper - lower)))
)
)
return "".join(sample(password, len(password)))
if __name__ == '__main__':
print mkpasswd()

to this article on the Python randomly generated 8-bit password example explains the article is introduced to this, more related Python randomly generated password content, please search for my previous posts or continue to browse the following related articles I hope that you will support me more in the future!