【Python】學習筆記4-random、string模組

來源:互聯網
上載者:User

標籤:letters   strong   ring   lower   upper   bsp   字母   orm   choice   

1、random隨機數

 1 import random,string 2 print(random.random())#隨機浮點數,預設取0-1,不能指定範圍 3 print(random.randint(1,20))#隨機整數 4 print(random.randrange(1,20))#隨機產生一個range 5 print(random.choice(‘x23serw4‘))#隨機取一個元素 6 print(random.sample(string.digits,2))#從序列中隨機取幾個元素 7 print(random.uniform(1,9))#隨機取浮點數,可以指定範圍 8 print(round(random.uniform(1,9),5))#隨機取浮點數,可以指定範圍 #round保留幾位小數 9 x = [1,2,3,4,6,7]10 random.shuffle(x)#洗牌,打亂順序,會改變原list的值,本身沒有傳回值11 print(x)12 print(string.ascii_letters+string.digits)#所有的數字和字母

 

2、string取大小寫字母特殊字元

1 s = string.digits #0-9所有的數字2 s = string.ascii_letters #所有的大小寫字母3 s = string.ascii_lowercase #所有的小寫字母4 s = string.ascii_uppercase #所有的大寫字母5 s = string.punctuation #所有的特殊字元6 print(s)

 

【Python】學習筆記4-random、string模組

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.