import random #对应从'0'到'9'的ASCII码 48~57 #对应从'A'到'Z'的ASCII码 65~90 #对应从'a'到'z'的ASCII码 97~122 def get_aZ(): num=random.randint(65,122) if num<91 or num>96: return chr(num) else: return get_aZ() #方法一 list_1=[] for i in range(0,5): a=get_aZ() b=str(random.randint(0,9)) list_1.append(a) list_1.append(b) random.shuffle(list_1) print ''.join(list_1)
#结果 3p44im3M8i
#方法二 str='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' max=len(str)+1 list_2=[] for i in range(10): pos=random.randint(0,max) list_2.append(str[pos-1:pos]) print ''.join(list_2)
#结果 VKrhmry1bJ