Python function (uppercase, lowercase, numeric) that randomly produces a specific type of character

Source: Internet
Author: User
Tags chr ord uppercase letter

1. Create a randomcharacter.py file (to be used as a module file for testing or implement program invocation) as follows:


From random import Randint

# generate a random character between Ch1 and CH2 (generate CH1 characters to CH2 characters any character)

def getrandomcharacter (CH1,CH2):

Return Chr (Randint (Ord (CH1), Ord (CH2)))


# Generate a random lowercase letter (generates any lowercase characters)

Def getrandomlowercaseletter ():

Return Getrandomcharacter (' A ', ' Z ')

# Generate a random uppercase letter (generates any uppercase characters)

Def getrandomuppercaseletter ():

Return Getrandomcharacter (' A ', ' Z ')


# Generate a random digit character (generates any 0-9 numeric characters)

Def getrandomdigitcharacter ():

return Getrandomcharacter (' 0 ', ' 9 ')


# Generate a random character

Def getrandomassicharacter ():

Return Chr (Randint (0,127))


2. Test program testrandom.py, call Randomcharacter module,


Import Randomcharacter

From random import Randint


Number_of_chars = 175

Char_per_line = 25


For I in Range (Number_of_chars):

Print (Randomcharacter.getrandomlowercaseletter (), end= "")

if (i + 1)% Char_per_line = = 0:

Print ()


Test program function: Generate 6 lines per line 25 total of 175 lowercase characters.





This article is from the "Bean Daddy Blog" blog, please make sure to keep this source http://imlearner.blog.51cto.com/9615100/1970335

Python function (uppercase, lowercase, numeric) that randomly produces a specific type of character

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.