Four-bit random verification code, the use of Chr function
B=[] for in range (4): import random c= Random.randint (97,122)# only lowercase ascll code range b.append (Chr (c)) d=' . Join (b) Print (d)
Four-bit random verification code, including case and number
codelist=[]ImportRandom forIinchRange (48,58):#the digital range of the ASCLL code can also be directly used with the digital 1-10Codelist.append (str (CHR (i)))#If you use numbers directly, use STR to convert the numbers directly into strings. forIinchRange (65,91):#Uppercasecodelist.append (str (CHR (i))) forIinchRange (97,123):#lowercasecodelist.append (str (CHR (i))) C=random.sample (codelist,4)#randomly take four values from a listb="'. Join (c)#Convert a list to a stringPrint(b)
Random verification Code as a function
defcod (): codelist=[] ImportRandom forIinchRange (48,58):#the digital range of the ASCLL code can also be directly used with the digital 1-10Codelist.append (str (CHR (i)))#If you use numbers directly, use STR to convert the numbers directly into strings. forIinchRange (65,91):#Uppercasecodelist.append (str (CHR (i))) forIinchRange (97,123):#lowercasecodelist.append (str (CHR (i))) C=random.sample (codelist,4)#randomly take four values from a listb="'. Join (c)#Convert a list to a string returnBA=cod ()Print(a)
Python randomly generates verification codes, applying the random and CHR functions