Python randomly generates a password with a specified length.

Source: Internet
Author: User

Python randomly generates a password with a specified length.

This example describes how to randomly generate a password of a specified length in python. Share it with you for your reference. The details are as follows:

The following python code randomly combines various characters to generate a random password with a specified length

The string object in python has several common methods to output different characters:

String. ascii_letters

All characters that output the ascii code

String. digits

Output '123 '.

String. punctuation

Punctuation in ascii

Print string. ascii_lettersprint string. digitsprint string. punctuation

The output result is as follows:
AbcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
0123456789
! "# $ % & '() * +,-./:; <=>? @ [\] ^ _ '{| }~

The following code is used to generate a random password

Import stringfrom random import * characters = string. ascii_letters + string. punctuation + string. digitspassword = "". join (choice (characters) for x in range (randint (8, 16) print password

I hope this article will help you with Python programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.