Set str as a string
##str.isalnum() All characters are numbers or letters(Recommended learning: Python video Tutorial )
str.isalpha() All characters are letters
str.isdigit() All characters are numbers
str.islower() All characters are lowercase
str.isupper() All characters are uppercase
str.istitle() All words are capitalized
str.isspace() All characters are blank characters or \t or \n or \r
Example:
"""从键盘上输入 一个字符,判断其字符类型。""" while True: char = input("请输入需要判断的字符:") if str.isdigit(char) == True: print("该字符为数字") try: char = int(char) print("并且该数值类型为int") except: pass elif str.isalpha(char) == True: if char >= u'\u4e00' and char <= u'\u9fa5': # 判断该字符是否为汉字 print("该字符是汉字") else: print("该字符是字母") elif str.isalnum(char) == True: print("该字符为数字和字母组合") elif char == " ": print("该字符为空格") else: try: char = float(char) print("该字符为数字") print("并且该数值类型为float") except: print("该字符为其他")
Python Tutorial column to learn!
The above is the detailed content of How to determine character type in Python. For more information, please follow other related articles on the PHP Chinese website!