python随机大小写字符串_python 随机产生特定类型字符的函数(大写、小写、数字)...
1、創建一個 Randomcharacter.py? 文件(作為模塊文件供測試或實現程序調用)內容如下:
from random import randint
# generate a random character between ch1 and ch2(產生ch1字符到ch2字符之間任意一字符)
def getRandomCharacter(ch1,ch2):
return chr(randint(ord(ch1),ord(ch2)))
# generate a random lowercase letter(產生任意一小寫字符)
def getRandomLowerCaseLetter():
return getRandomCharacter('a','z')
# generate a random uppercase letter(產生任意一大寫字符)
def getRandomUpperCaseLetter():
return getRandomCharacter('A','Z')
# generate a random digit character(產生任意一0-9的數字字符)
def getRandomDigitCharacter():
return getRandomCharacter('0','9')
# generate a random? character
def getRandomASSICharacter():
return chr(randint(0,127))
2. 測試程序 TestRandom.py,調用Randomcharacter模塊,
import RandomCharacter
from random import randint
NUMBER_OF_CHARS = 175
CHAR_PER_LINE = 25
for i in range(NUMBER_OF_CHARS):
print(RandomCharacter.getRandomLowerCaseLetter(),end="")
if (i + 1) % CHAR_PER_LINE == 0:
print()
測試程序功能:產生6行每行25個共175個小寫字符。
總結
以上是生活随笔為你收集整理的python随机大小写字符串_python 随机产生特定类型字符的函数(大写、小写、数字)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python生成多级文件夹_Python
- 下一篇: python123子字符串输出_Pyth