兄弟們,今天我們來用Python生成隨機密碼試試~

知識點
- 檔案讀寫
- 基礎語法
- 字串處理
- 字符拼接
代碼決議
匯入模塊
import platform import string import random # 我還給大家準備了這些資料:Python視頻教程、100本Python電子書、基礎、爬蟲、資料分析、web開發、機器學習、人工智能、面試題、Python學習路線圖、問題解答! # 都放在這個扣群啦:279199867
將string的幾大字串拼接在一起,作為候選,
words = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation len = int(input("請輸入密碼位數:"))
根據長度隨機采樣幾個字符,得到一個串列,
chosen = random.sample(words, len)
將串列的每個元素,拼接成一個大字串,
password = "".join(chosen)
補充String模塊中的常量:
- 小寫字母:string.ascii_lowercase;
- 大寫字母:string.ascii_uppercase;
- 數字:string.digits;
- 標點符號:string.punctuation
全部代碼
import platform import string import random print("古有前輩壯志饑餐胡虜肉,笑談渴飲匈奴血,今有我輩壯志饑餐鬼子肉,笑談渴飲大和血") print("實戰場景: 如何生成隨機密碼 \n") words = string.ascii_lowercase + string.ascii_uppercase + string.digits + string.punctuation len = int(input("請輸入密碼位數:")) chosen = random.sample(words, len) password = "".join(chosen) print(password) print("Python 版本", platform.python_version())
效果展示
我輸入個6 先試試

可以看到,兩次都是完全不同的密碼,效果一級棒!


兄弟們,快去試試吧!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/500384.html
標籤:Python
上一篇:Python 元類詳解
下一篇:Matplotlib(基本用法)
