Python produces random numbers, random strings

Source: Internet
Author: User

Import Random
Import string
#随机整数:
Print Random.randint (1,50)
#随机选取0到100间的偶数:
Print Random.randrange (0, 101, 2)
#随机浮点数:
Print Random.random ()
Print Random.uniform (1, 10)
#随机字符:
Print Random.choice (' [Email protected]#$%^&* () ')
#多个字符中选取特定数量的字符:
Print random.sample (' ZYXWVUTSRQPONMLKJIHGFEDCBA ', 5)
#多个字符中选取特定数量的字符组成新字符串:
Print String.Join (random.sample ([' Z ', ' y ', ' x ', ' w ', ' V ', ' u ', ' t ', ' s ', ' r ', ' Q ', ' P ', ' O ', ' n ', ' m ', ' l ', ' K ', ' j ', ' I ', ' h ', ' g ', ' f ', ' e ', ' d ', ' C ', ' B ', ' A '], 5). Replace (",")
#随机选取字符串:
Print Random.choice ([' Scissors ', ' stone ', ' cloth '])
#打乱排序
Items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Print Random.shuffle (items)

Python produces random numbers, random strings

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.