练习3:生成随机的字母和数字,需要类似格式: 字母数字字母数字字母数字,生成长度为10的字符串 >>> chr(random.randint(97,122)) #随机生成一个字母 'o' >>> random.randint(0,9) #随机生成一个数字 7 >>> chr(random.randint(97,122))+str(random.randint(0,9)) #拼接,随机生成一对字母加数字 'm7' import random >>> result ="" >>> for i in range(5): ... result+=chr(random.randint(97,122))+str(random.randint(0,9)) ... >>> print(result) e4f2p4b4z0 方法2: import random result ="" for i in range(10): if i %2 ==0: result+=chr(random.randint(97,122)) else: result+=str(random.randint(0,9)) print(result) 方法3: import random import string result ="" for i in range(5): result+=string.ascii_letters[random.randint(0,52)]\ +string.digits[random.randint(0,9)] print(result)