str.isdecimal() and str.isdigit() difference example

There are differences, but they’re somewhat rare*. It mainly crops up with various unicode characters, such as 2:

>>> c="\u00B2"
>>> c.isdecimal()
False
>>> c.isdigit()
True

You can also go further down the careful-unicode-distinction rabbit hole with the isnumeric method:

>>> c="\u00BD" # ½
>>> c.isdecimal()
False
>>> c.isdigit()
False
>>> c.isnumeric()
True

*At least, I’ve never encountered production code that needs to distinguish between strings that contain different types of these exceptional situations, but surely use cases exist somewhere.

Leave a Comment