python—模組-random

來源:互聯網
上載者:User

標籤:模組   隨機   random   一個   bcd   pytho   ice   執行個體   字串   

程式中有很多地方需要用到隨機字元,比如登入網站的隨機驗證碼,python中通過random模組可以很容易產生隨機字串。

執行個體:大樂透,公司年會抽獎

>>> import random>>> random.randint(1,100)  #1-100中間取一個隨機數,包含10032>>> random.randint(1,100)59>>> random.randint(1,100)32>>>random.randrange(1-100)  #1-100中間取一個隨機數,不包含10022>>>random.choice(‘da124#!‘)  #從字串裡返回隨機的東西,拼隨機驗證碼可能用的到‘4‘>>>random.sample(‘da124#!‘,3)   #返回3個隨機值[‘#‘, ‘4‘, ‘1‘]>>> import string>>> string.digits‘0123456789‘>>> string.ascii_letters‘abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ‘>>> string.hexdigits‘0123456789abcdefABCDEF‘

  

python—模組-random

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.