溫馨提示×

python怎么生成隨機密碼

小億
156
2023-08-24 23:22:30
欄目: 編程語言

Python中可以使用random模塊來生成隨機密碼。下面是一個生成隨機密碼的示例代碼:

import random
import string
def generate_password(length):
chars = string.ascii_letters + string.digits + string.punctuation
password = ''.join(random.choice(chars) for _ in range(length))
return password
length = int(input("請輸入密碼長度:"))
password = generate_password(length)
print("隨機密碼為:", password)

運行代碼后,程序會要求輸入密碼長度,然后生成相應長度的隨機密碼。random.choice(chars)函數用于從chars字符串中隨機選擇一個字符,''.join()函數用于將選擇的字符連接起來形成密碼。string.ascii_letters表示所有的字母,string.digits表示所有的數字,string.punctuation表示所有的標點符號。你也可以根據需要修改chars字符串來控制密碼所包含的字符類型。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女