Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Reading python docs I have come to .isdecimal() and .isdigit() string functions and i'm not finding literature too clear on their usable distinction. Could someone supply me with code examples of where these two functions differentiate please.

Similar behaviour:

>>> str.isdecimal('1') True >>> str.isdigit('1') True  >>> str.isdecimal('1.0') False >>> str.isdigit('1.0') False  >>> str.isdecimal('1/2') False >>> str.isdigit('1/2') False 
like image 900
Phoenix Avatar asked Apr 01 '14 14:04

Phoenix


People also ask

What is the difference between Isdigit Isnumeric and Isdecimal in Python?

isdecimal() vs isdigit() vs isnumeric()isdecimal() method supports only Decimal Numbers. isdigit() method supports Decimals, Subscripts, Superscripts. isnumeric() method supports Digits, Vulgar Fractions, Subscripts, Superscripts, Roman Numerals, Currency Numerators.

What is difference between Isnumeric and Isdigit?

The Python isnumeric method has a number of key differences between the Python isdigit method. While the isidigit method checks whether the string contains only digits, the isnumeric method checks whether all the characters are numeric.

What is difference between digit and decimal?

Digits include decimal characters and digits that need special handling, such as the compatibility superscript digits. This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers. Formally, a digit is a character that has the property value Numeric_Type=Digit or Numeric_Type=Decimal.

Does Isdigit include 0?

isdigit only returns True for what I said before, strings containing solely the digits 0-9.


1 Answers

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.

like image 124
Henry Keiter Avatar answered Sep 18 '22 13:09

Henry Keiter