python3----輸出全部大小寫字母及數字

1. 用一行輸出全部大(小)寫字母,以及數字git

1 print([chr(i) for i in range(65, 91)])  # 全部大寫字母
2 print([chr(i) for i in range(97, 123)])  # 全部小寫字母
3 print([chr(i) for i in range(48, 58)])   # 全部數字
4 
5 ####################
6 ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z']
7 ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
8 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

 

 1 import string   # 導入string這個模塊
 2 print(string.digits)  # 輸出包含數字0~9的字符串
 3 print(string.ascii_letters)  # 包含全部字母(大寫或小寫)的字符串
 4 print(string.ascii_lowercase)  # 包含全部小寫字母的字符串
 5 print(string.ascii_uppercase)  # 包含全部大寫字母的字符串
 6 
 7 
 8 ##############
 9 0123456789
10 abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
11 abcdefghijklmnopqrstuvwxyz
12 ABCDEFGHIJKLMNOPQRSTUVWXYZ

2. 列表生成式app

1 a = [str(x) for x in range(10)]
2 
3 ######################
4 ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']

 

 

3. 生成隨機驗證碼dom

1 import random
2 def get_code():
3     source = list('0123456789')
4     for i in range(97, 123):
5         source.append(chr(i))
6     print(''.join(random.sample(source, 4)))

 

1 def v_code():
2     code = ''
3     for i in range(5):
4         add = random.choice([random.randrange(10), chr(random.randrange(97, 123))])
5         code += str(add)
6     print(code)
相關文章
相關標籤/搜索