Difference between str.isdigit() and str.isdecimal() in Python 3

M

Marco

Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but
I can't find it:
(False, False)

Can anyone give me some help?
Regards, Marco
 
U

Ulrich Eckhardt

Marco said:
(False, False)

[chr(a) for a in range(0x20000) if chr(a).isdigit()]

Congratulations, you found a bug! Or maybe not, it all depends on whether
Roman numbers are considered digits or not. I could imagine there being a
difference.

:)

Uli
 
J

jmfauth

Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but
I can't find it:

 >>> '123'.isdecimal(), '123'.isdigit()
(True, True)
 >>> print('\u0660123')
Ù 123
 >>> '\u0660123'.isdigit(), '\u0660123'.isdecimal()
(True, True)
 >>> print('\u216B')
â…«
 >>> '\u216B'.isdecimal(), '\u216B'.isdigit()
(False, False)

Can anyone give me some help?
Regards, Marco

It seems to me that it is correct, and the reason lies in this:
12.0

jmf
 
T

Thomas 'PointedEars' Lahn

Marco said:
Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but
I can't find it:

(False, False)

Can anyone give me some help?

RTFM.

$ python3 -c 'print("42".isdecimal.__doc__ + "\n");
print("42".isdigit.__doc__)'
S.isdecimal() -> bool

Return True if there are only decimal characters in S,
False otherwise.

S.isdigit() -> bool

Return True if all characters in S are digits
and there is at least one character in S, False otherwise.
 
S

Steven D'Aprano

Hi all, because

"There should be one-- and preferably only one --obvious way to do it",

there should be a difference between the two methods in the subject, but
I can't find it:

The Fine Manual has more detail, although I admit it isn't *entirely*
clear what it is talking about if you're not a Unicode expert:


http://docs.python.org/py3k/library/stdtypes.html#str.isdecimal

str.isdecimal()
Return true if all characters in the string are decimal characters
and there is at least one character, false otherwise. Decimal characters
are those from general category “Ndâ€. This category includes digit
characters, and all characters that can be used to form decimal-radix
numbers, e.g. U+0660, ARABIC-INDIC DIGIT ZERO.

str.isdigit()
Return true if all characters in the string are digits and there is
at least one character, false otherwise. Digits include decimal
characters and digits that need special handling, such as the
compatibility superscript digits. Formally, a digit is a character that
has the property value Numeric_Type=Digit or Numeric_Type=Decimal.


And also:

str.isnumeric()
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.


Examples:

py> c = '\u2155'
py> print(c)
â…•
py> c.isdecimal(), c.isdigit(), c.isnumeric()
(False, False, True)
py> import unicodedata
py> unicodedata.numeric(c)
0.2

py> c = '\u00B2'
py> print(c)
²
py> c.isdecimal(), c.isdigit(), c.isnumeric()
(False, True, True)
py> unicodedata.numeric(c)
2.0
 
M

Marco

the Fine Manual has more detail, although I admit it isn't *entirely*
clear what it is talking about if you're not a Unicode expert:


http://docs.python.org/py3k/library/stdtypes.html#str.isdecimal

You are right, that is clear, thanks :)
Examples:

py> c = '\u2155'
py> print(c)
â…•
py> c.isdecimal(), c.isdigit(), c.isnumeric()
(False, False, True)
py> import unicodedata
py> unicodedata.numeric(c)
0.2

py> c = '\u00B2'
py> print(c)
²
py> c.isdecimal(), c.isdigit(), c.isnumeric()
(False, True, True)
py> unicodedata.numeric(c)
2.0

Perfect explanation, thanks again, Marco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top