python-Generating Random characters

Source: Internet
Author: User
Tags for in range ord

Demand:

Randomly generate 6-bit uppercase letters:

Method One:

# !/usr/bin/env  python#  -*-coding:utf-8-*-import= []   for in range (6):    = Random.randrange (65,91)    = chr (temp)    " ". Join (LI)print(Result)

Result: 6 letters are randomly generated

The function is implemented, but it feels a little low, for example: the generated characters are relatively single, only letters can be generated. I was wondering if I could get two numbers in 6 characters? Okay, let's get started.

Method Two:

#!/usr/bin/env python#-*-coding:utf-8-*-ImportRandomli= [] forIinchRange (6):    ifi = = 2ori = = 4: Num= Random.randrange (0,10) li.append (num) #将数字转换为字符串 because the. Join () method cannot handle numeric types, only data of character types can appear Else: Temp= Random.randrange (65,91) C=chr (temp) li.append (c) Result="". Join (LI)Print(Result)

Results:

dm5t9b

Method two satisfies the requirement and includes 2 digits in 6 characters. But we have not found that the position of two numbers is fixed, which is not in line with the logic of normal people ah. Ask for random, random, random. OK, we know the number of times and the position of the number and the letter is random. Start doing it.

Method Three:

#!/usr/bin/env python#-*-coding:utf-8-*-ImportRandomli= [] forIinchRange (6): R= Random.randrange (0,5)    ifr = = 2orr = = 4: Num= Random.randrange (0,10) li.append (num)Else: Temp= Random.randrange (65,91) C=chr (temp) li.append (c) Result="". Join (LI)Print(Result)

Results:

384m4j

Method Three: Optimization of method Two

#!/usr/bin/env python#-*-coding:utf-8-*-ImportRandomli= [] forIinchRange (6): R= Random.randrange (0,5)    ifi = =R:num= Random.randrange (0,10) li.append (num)Else: Temp= Random.randrange (65,91) C=chr (temp) li.append (c) Result="". Join (LI)Print(Result)

Results:

N7wihl

Well, the demand has been fulfilled.

Summarize:

(1) Random numbers are generated randomly.

(2) i = Random.randrange (0,5) is a random number that generates 0-4, remember not to include 5 oh. Range of values: 1=< I < 5

(3) "". Join (LI) means that the value of the list Li is generated in the middle of a string without any symbolic separation, so the ""

For example:

Li = ['A','B','C',' D ','E'"". Join (LI)print( Result

Result: "" is used here. Join ()

ABCDE

We use the "_" underline to separate. Use "_". Join ()

Li = ['A','B','C',' D ','E'"_". Join (LI)  Print(Result)

Results:

A_b_c_d_e

(4) Join () can only handle characters, cannot handle numbers, do not believe? OK, then let's try it.

# !/usr/bin/env  python#  -*-coding:utf-8-*-li = [1,2,3,4,5]_  ". Join (LI)print(Result)

Results:

So we used the code: STR ()

num = Random.randrange (0,10) li.append (num)  #将数字转换为字符串 because the. Join () method cannot handle numeric types. Only character-type data can appear 

(5) CHR and Ord

CHR converts the number to the corresponding letter in the ASII code.

Ord converts the letter to the corresponding number in the Asii code.

R = Chr (+)print= Ord ("A")print(n)

Results:

A
65

python-Generating Random characters

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.