Python's str type also features a number of methods that can be used to evaluate the contents of a string. These are str.isalpha, str.isdigit, str.isalnum, str.isspace. Capitalization can be tested with str.isupper, str.islower and str.istitle.
str.isalphastr.isalpha takes no arguments and returns True if the all characters in a given string are alphabetic, for example:
>>> "Hello World".isalpha() # contains a space
False
>>> "Hello2World".isalpha() # contains a number
False
>>> "HelloWorld!".isalpha() # contains punctuation
False
>>> "HelloWorld".isalpha()
True
As an edge case, the empty string evaluates to False when used with "".isalpha().
str.isupper, str.islower, str.istitleThese methods test the capitalization in a given string.
str.isupper is a method that returns True if all characters in a given string are uppercase and False otherwise.
>>> "HeLLO WORLD".isupper()
False
>>> "HELLO WORLD".isupper()
True
>>> "".isupper()
False
Conversely, str.islower is a method that returns True if all characters in a given string are lowercase and False otherwise.
>>> "Hello world".islower()
False
>>> "hello world".islower()
True
>>> "".islower()
False
str.istitle returns True if the given string is title cased; that is, every word begins with an uppercase character followed by lowercase characters.
>>> "hello world".istitle()
False
>>> "Hello world".istitle()
False
>>> "Hello World".istitle()
True
>>> "".istitle()
False
str.isdecimal, str.isdigit, str.isnumericstr.isdecimal returns whether the string is a sequence of decimal digits, suitable for representing a decimal number.
str.isdigit includes digits not in a form suitable for representing a decimal
number, such as superscript digits.
str.isnumeric includes any number values, even if not digits, such as values outside the range 0-9.
isdecimal isdigit isnumeric
12345 True True True
១2߃໔5 True True True
①²³🄅₅ False True True
⑩⒓ False False True
Five False False False
Bytestrings (bytes in Python 3, str in Python 2), only support isdigit, which only checks for basic ASCII digits.
As with str.isalpha, the empty string evaluates to False.
str.isalnumThis is a combination of str.isalpha and str.isnumeric, specifically it evaluates to True if all characters in the given string are alphanumeric, that is, they consist of alphabetic or numeric characters:
>>> "Hello2World".isalnum()
True
>>> "HelloWorld".isalnum()
True
>>> "2016".isalnum()
True
>>> "Hello World".isalnum() # contains whitespace
False
str.isspaceEvaluates to True if the string contains only whitespace characters.
>>> "\t\r\n".isspace()
True
>>> " ".isspace()
True
Sometimes a string looks “empty” but we don't know whether it's because it contains just whitespace or no character at all
>>> "".isspace()
False
To cover this case we need an additional test
>>> my_str = ''
>>> my_str.isspace()
False
>>> my_str.isspace() or not my_str
True
But the shortest way to test if a string is empty or just contains whitespace characters is to use strip(with no arguments it removes all leading and trailing whitespace characters)
>>> not my_str.strip()
True