1 """從鍵盤上輸入 一個字符,判斷其字符類型。"""
2 while True: 3 char = input("請輸入需要判斷的字符:") 4 if str.isdigit(char) == True: 5 print("該字符為數字") 6 try: 7 char = int(char) 8 print("並且該數值類型為int") 9 except: 10 pass
11
12 elif str.isalpha(char) == True: 13 """關於判斷漢字方法查閱資料 原文鏈接:https://blog.csdn.net/guotong1988/article/details/80896663"""
14 if char >= u'\u4e00' and char <= u'\u9fa5': # 判斷該字符是否為漢字
15 print("該字符是漢字") 16 else: 17 print("該字符是字母") 18 elif str.isalnum(char) == True: 19 print("該字符為數字和字母組合") 20 elif char == " ": 21 print("該字符為空格") 22 else: 23 try: 24 char = float(char) 25 print("該字符為數字") 26 print("並且該數值類型為float") 27 except: 28 print("該字符為其他")