Python: String, Check Case, Char Class

By Xah Lee. Date: .

Check Character Case, Character Class

str.isalnum()
Return True if all characters are alphanumeric and there is at least one character, else False.
str.isalpha()
str.isdigit()
str.isupper()
str.islower()
str.isspace()
Check whitespace characters.
str.istitle()
True if every word start with cap letter.
unicode.isnumeric()
Return True if there are only numeric characters in S, False otherwise.

Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH.

unicode.isdecimal()
Return True if there are only decimal characters in unicode, False otherwise.

Decimal characters include digit characters, and all characters that can be used to form decimal-radix numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.

str.translate(table, ?deleteChars)
Return a copy of the string where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.

You can use the maketrans() helper function in the string module to create a translation table. For string objects, set the table argument to None for translations that only delete characters:

Python, String