Python randomly generates a 6-digit verification code.

Source: Internet
Author: User

Python randomly generates a 6-digit verification code.
1. Example

#-*-Coding: UTF-8-*-import randomdef generate_verification_code (): ''' a 6-digit verification code is randomly generated ''' code_list = [] for I in range (10 ): #0-9 number code_list.append (str (I) for I in range (65, 91): # A-Z code_list.append (chr (I) for I in range (97,123 ): # a-z code_list.append (chr (I) myslice = random. sample (code_list, 6) # randomly obtain 6 elements from the list and return verification_code = ''as a piece ''. join (myslice) # list to string # print code_list # print type (myslice) return verification_codedef generate_verification_code2 (): '''randomly generate a 6-digit Verification Code ''' code_list = [] for I in range (2): random_num = random. randint (0, 9) # randomly generate 0-9 numbers # use random. the randint () function generates a random integer a so that 65 <= A <= 90 # Corresponds to the ASCII code a = random from "a" to "Z. randint (65, 90) B = random. randint (97,122) Comment = chr (a) Comment = chr (B) code_list.append (str (random_num) code_list.append (comment) code_list.append (random_lowercase_letter) verification_code = ''. join (code_list) return verification_codeif _ name _ = '_ main _': code = generate_verification_code () code2 = generate_verification_code2 () print code print code2

One of the results is as follows:

GF5UzK
2Cb1Aa

Related Article

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.