Home > Backend Development > Python Tutorial > python随机生成指定长度密码的方法

python随机生成指定长度密码的方法

WBOY
Release: 2016-06-06 11:23:28
Original
1480 people have browsed it

本文实例讲述了python随机生成指定长度密码的方法。分享给大家供大家参考。具体如下:

下面的python代码通过对各种字符进行随机组合生成一个指定长度的随机密码

python中的string对象有几个常用的方法用来输出各种不同的字符:

string.ascii_letters
Copy after login

输出ascii码的所有字符

string.digits
Copy after login

输出 '0123456789'.

string.punctuation
Copy after login

ascii中的标点符号

print string.ascii_letters
print string.digits
print string.punctuation
Copy after login

输出结果如下:
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

下面的代码用于生成随机密码

import string
from random import *
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(characters) for x in range(randint(8, 16)))
print password
Copy after login

希望本文所述对大家的Python程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template