"""今天写一个程序,在想既然可以获得随机数,那我可不可以获得任意字符呢,于是在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 randomKEY_LEN = 20def 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 Qgr7OEalfhXllcFDGh2lUbuntu 14.04安装Python 3.3.5 http://www.linuxidc.com/Linux/2014-05/101481.htmCentOS上源码安装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.htmPython脚本获取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本文永久更新链接地址:http://www.linuxidc.com/Linux/2016-12/138500.htm