What is the difference between isnumeric and isdecimal functions for strings ( https://www.tutorialspoint.com/python3/python_strings.htm )? They seem to give identical results:
>>> "123456".isnumeric()
True
>>> "123456".isdecimal()
True
>>> "123.456".isnumeric()
False
>>> "123.456".isdecimal()
False
>>> "abcd".isnumeric()
False
>>> "abcd".isdecimal()
False
I expected isdecimal()
to return true for "123.456" but it does not.
Python String isdecimal() The isdecimal() method returns True if all characters in a string are decimal characters. If not, it returns False.
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.
The method isdigit() checks whether the string consists of digits only. The method isalnum() checks whether the string consists of alphanumeric characters. Both return true, because for isdigit(), the string contains purely digits. For isalnum(), all characters are either alpha or numeric.
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.
The two methods test for specific Unicode character classes. If all characters in the string are from the specified character class (have the specific Unicode property), the test is true.
isdecimal()
does not test if a string is a decimal number. See the documentation:
Return true if all characters in the string are decimal characters and there is at least one character, false otherwise. Decimal characters are those that can be used to form numbers in base 10, e.g. U+0660, ARABIC-INDIC DIGIT ZERO. Formally a decimal character is a character in the Unicode General Category “Nd”.
The .
period character is not a member of the Nd
category; it is not a decimal character.
str.isdecimal()
characters are a subset of str.isnumeric()
; this tests for all numeric characters. Again, from the documentation:
Return true if all characters in the string are numeric characters, and there is at least one character, false otherwise. Numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.
Nd
is Numeric_Type=Digit
here.
If you want to test if a string is a valid decimal number, just try to convert it to a float:
def is_valid_decimal(s):
try:
float(s)
except ValueError:
return False
else:
return True
There are many more numeric characters than decimal ones:
>>> "٣".isdecimal()
True
>>> "¼".isdecimal()
False
>>> "¼".isnumeric()
True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With