Skip to content
Home » The isdecimal, isdigit and isnumeric Methods

The isdecimal, isdigit and isnumeric Methods

Spread the love

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:

1) formatting methods

2) statistical methods

3) searching methods

4) boolean methods

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:

Categories of Number Characters

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

Your Panda3D Magazine

Make Awesome Games and Other 3D Apps

with Panda3D and Blender using Python.

Cool stuff, easy to follow articles.

Get the magazine here (PDF).

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

Python Jumpstart Course

Learn the basics of Python, including OOP.

with lots of exercises, easy to follow

The course is available on Udemy.

Blender Jumpstart Course

Learn the basics of 3D modeling in Blender.

step-by-step, easy to follow, visually rich

The course is available on Udemy and on Skillshare.


Spread the love

Leave a Reply