Home>Article>Backend Development> How to determine character type in Python

How to determine character type in Python

(*-*)浩
(*-*)浩 Original
2019-07-04 14:21:55 14082browse

Set str as a string

How to determine character type in Python

##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("该字符为其他")
For more Python-related technical articles, please visit the

Python Tutorialcolumn 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn