"""今天寫一個程序,在想既然可以獲得隨機數,那我可不可以獲得任意字符呢,於是在stackoverflow.com 上找到了方法,幾乎都是用導入random,然後再用其它方法間接實現隨機字符。
現在總結如下:"""
#獲取單個
>>> import random
>>> import string
>>> random.choice(string.ascii_lowercase)
'b'
>>> import string
>>> string.letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
>>> import random
>>> random.choice(string.letters)
'j'
import random
def guess_letter():
return random.choice('abcdefghijklmnopqrstuvwxyz')
import string #This was a design above but failed to print. I remodled it.
import random
irandom = random.choice(string.ascii_letters)
print irandom
#獲取一串:
>>>def random_char(y):
return ''.join(random.choice(string.ascii_letters) for x in range(y))
>>>print (random_char(5))
>>>fxkea
可以簡化為:''.join(random.sample(string.ascii_lowercase,5))
import string
import random
KEY_LEN = 20
def base_str():
return (string.letters+string.digits)
def key_gen():
keylist = [random.choice(base_str()) for i in range(KEY_LEN)]
return ("".join(keylist))
You can get random strings like this:
g9CtUljUWD9wtk1z07iF
ndPbI1DDn6UvHSQoDMtd
klMFY3pTYNVWsNJ6cs34
Qgr7OEalfhXllcFDGh2l
Ubuntu 14.04安裝Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htm
CentOS上源碼安裝Python3.4 http://www.linuxidc.com/Linux/2015-01/111870.htm
《Python核心編程 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm
《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm
Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
在Ubuntu下用Python搭建桌面算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm
在CentOS 6.5上安裝Python2.7 http://www.linuxidc.com/Linux/2016-10/136206.htm