In one of my previous articles we were talking about the methods isalnum, isalpha and isspace. They are used to check whether a given string contains only a specific type of characters, like only letters, digits or white spaces. You can read more about them here.
You may also want to read more about other string methods. I’ve written a couple posts about them. Here they are:
Here’s the video version of the article:
Categories of Number Characters
It turns out, there are more methods like these. In this article we’ll be talking about a couple of methods used to check if the string contains only digits. But actually, there are a couple of categories of number characters in Unicode. Let’s have a look at them first:
– Number, Decimal Digit – this category includes all the digits that we use 0-9, but also other characters that are used as numbers in other languages, like for instance the numbers used in Arabic-speaking countries ٠, ١, ٢, ٣, ٤, etc. and many more.
– Number, Other – this category includes lots of other numeric symbols, which are not pure digits like the ones in the decimal category. Here we have, among others, symbols like: ² (superscript), ¼ (fraction), ①, ⓮ (circled digits), and many more.
– Number, Letter – this category includes letter symbols used as numbers, for example Roman numerals or cuneiform numeric signs among others.
Here’s a summary for a quick reference:
The isdecimal, isdigit and isnumeric Methods
In Python we have a couple of methods to check if a string contains only numeric characters:
The isdecimal method returns True if all the characters are decimal, i.e. belong to the Number, Decimal Digit category:
>>> "2018".isdecimal()
True
>>> "5¼".isdecimal() # ¼ is not in the Number, Decimal Digit category
False
>>> "²".isdecimal() # ² is not in the Number, Decimal Digit category
False
The isdigit method returns True for all the characters in the Number, Decimal Digit category, so all the characters, for which isdecimal returns True, and also for some characters from the Number, Other category:
>>> "2018".isdigit()
True
>>> "²".isdigit()
True
>>> "5²".isdigit()
True
>>> "①".isdigit()
True
but not for all of them:
>>> "5¼".isdigit()
False
The isnumeric method returns True if the string contains only numeric characters belonging to any of the above described categories, so decimal, letter and other numeric characters:
>>> "25".isnumeric()
True
>>> "¼".isnumeric()
True
>>> "Ⅷ".isnumeric()
True