python--隨機生成8為大小寫字母數字組成的密碼


import random,string    #調用random、string模塊


def create_password():

    src_digits = string.digits              #string_數字
    src_uppercase = string.ascii_uppercase  #string_大寫字母
    src_lowercase = string.ascii_lowercase  #string_小寫字母
    digits_num = random.randint(1,6)
    uppercase_num = random.randint(1,8-digits_num-1)
    lowercase_num = 8 - (digits_num + uppercase_num)

    #生成字符串
    password = random.sample(src_digits,digits_num) + random.sample(src_uppercase,uppercase_num) + random.sample(src_lowercase,lowercase_num)

    #打亂字符串
    random.shuffle(password)

    #列表轉字符串
    new_password = ''.join(password)
    print(new_password)
    return new_password

if __name__ == '__main__':
    create_password()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM