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

PythonPython 3.x

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

Lets see some examples:

> str.isdecimal() (Only Decimal Numbers)

Is 34 a decimal number? --> Yes

print("34".isdecimal())  #True

Is superscript 2 a decimal number? --> No

print("\u00B2")
print("\u00B2".isdecimal())  #False

> str.isdigit() (Decimals, Subscripts, Superscripts)

Is 34 a digit? --> Yes

print("34".isdigit()) #True

Is superscript 2 a digit? --> Yes

print("\u00B2")
print("\u00B2".isdigit()) #True

> str.isnumeric() (Decimals, Subscripts, Superscripts, Vulgar Fractions, Roman Numerals, Currency Numerators)

Is 34 a numeric number? --> Yes

print("34".isnumeric()) #True

Is superscript 2 a numeric number? --> Yes

print("\u00B2")
print("\u00B2".isnumeric()) #True

Is Vulgar Fraction one Quarter numeric number? -->Yes

print("\u00BC")
print("\u00BC".isnumeric()) #True

Solution 3 - Python

If you doubt, my advice - to code, to look a results, to draw conclusions.

##A code

In [115]: import itertools
     ...: 
     ...: line = '-' * 37
     ...: print(line)
     ...: print("|    №   | isdigit | isdecimal | chr")
     ...: print(line)
     ...: for number in itertools.chain(range(1000), range(4969, 4978), range(8304, 11000)):
     ...:     char = chr(number)
     ...:     if (char.isdigit() or char.isdecimal()):
     ...:         print('| {0:>6} | {1:^7} | {2:^9} | {3:3} '.format(
     ...:             number,
     ...:             '+' if char.isdigit() else '-',
     ...:             '+' if char.isdecimal() else '-',
     ...:             char
     ...:         )
     ...:     )
     ...: 

##Look a results

-------------------------------------
|       | isdigit | isdecimal | chr
-------------------------------------
|     48 |    +    |     +     | 0   
|     49 |    +    |     +     | 1   
|     50 |    +    |     +     | 2   
|     51 |    +    |     +     | 3   
|     52 |    +    |     +     | 4   
|     53 |    +    |     +     | 5   
|     54 |    +    |     +     | 6   
|     55 |    +    |     +     | 7   
|     56 |    +    |     +     | 8   
|     57 |    +    |     +     | 9   
|    178 |    +    |     -     | ²   
|    179 |    +    |     -     | ³   
|    185 |    +    |     -     | ¹   
|   4969 |    +    |     -     |    
|   4970 |    +    |     -     |    
|   4971 |    +    |     -     |    
|   4972 |    +    |     -     |    
|   4973 |    +    |     -     |    
|   4974 |    +    |     -     |    
|   4975 |    +    |     -     |    
|   4976 |    +    |     -     |    
|   4977 |    +    |     -     |    
|   8304 |    +    |     -     |    
|   8308 |    +    |     -     |    
|   8309 |    +    |     -     |    
|   8310 |    +    |     -     |    
|   8311 |    +    |     -     |    
|   8312 |    +    |     -     |    
|   8313 |    +    |     -     |    
|   8320 |    +    |     -     |    
|   8321 |    +    |     -     |    
|   8322 |    +    |     -     |    
|   8323 |    +    |     -     |    
|   8324 |    +    |     -     |    
|   8325 |    +    |     -     |    
|   8326 |    +    |     -     |    
|   8327 |    +    |     -     |    
|   8328 |    +    |     -     |    
|   8329 |    +    |     -     |    
|   9312 |    +    |     -     |    
|   9313 |    +    |     -     |    
|   9314 |    +    |     -     |    
|   9315 |    +    |     -     |    
|   9316 |    +    |     -     |    
|   9317 |    +    |     -     |    
|   9318 |    +    |     -     |    
|   9319 |    +    |     -     |    
|   9320 |    +    |     -     |    
|   9332 |    +    |     -     |    
|   9333 |    +    |     -     |    
|   9334 |    +    |     -     |    
|   9335 |    +    |     -     |    
|   9336 |    +    |     -     |    
|   9337 |    +    |     -     |    
|   9338 |    +    |     -     |    
|   9339 |    +    |     -     |    
|   9340 |    +    |     -     |    
|   9352 |    +    |     -     |    
|   9353 |    +    |     -     |    
|   9354 |    +    |     -     |    
|   9355 |    +    |     -     |    
|   9356 |    +    |     -     |    
|   9357 |    +    |     -     |    
|   9358 |    +    |     -     |    
|   9359 |    +    |     -     |    
|   9360 |    +    |     -     |    
|   9450 |    +    |     -     |    
|   9461 |    +    |     -     |    
|   9462 |    +    |     -     |    
|   9463 |    +    |     -     |    
|   9464 |    +    |     -     |    
|   9465 |    +    |     -     |    
|   9466 |    +    |     -     |    
|   9467 |    +    |     -     |    
|   9468 |    +    |     -     |    
|   9469 |    +    |     -     |    
|   9471 |    +    |     -     |    
|  10102 |    +    |     -     |    
|  10103 |    +    |     -     |    
|  10104 |    +    |     -     |    
|  10105 |    +    |     -     |    
|  10106 |    +    |     -     |    
|  10107 |    +    |     -     |    
|  10108 |    +    |     -     |    
|  10109 |    +    |     -     |    
|  10110 |    +    |     -     |    
|  10112 |    +    |     -     |    
|  10113 |    +    |     -     |    
|  10114 |    +    |     -     |    
|  10115 |    +    |     -     |    
|  10116 |    +    |     -     |    
|  10117 |    +    |     -     |    
|  10118 |    +    |     -     |    
|  10119 |    +    |     -     |    
|  10120 |    +    |     -     |    
|  10122 |    +    |     -     |    
|  10123 |    +    |     -     |    
|  10124 |    +    |     -     |    
|  10125 |    +    |     -     |    
|  10126 |    +    |     -     |    
|  10127 |    +    |     -     |    
|  10128 |    +    |     -     |    
|  10129 |    +    |     -     |    
|  10130 |    +    |     -     | 

Draw a conclusions

As you can see, main difference between the function str.isdecimal() and str.isdigit() is that: the function str.isdecimal() return True only for numbers from 0 to 9, at the same time the function str.isdigit() return True for some other unicode-supported chars.

Solution 4 - Python

Completing the answer from PADYMKO, we can add the other function of interest and extend the code

Code

import itertools
line = '-' * 49
print(line)
print("|    №   | isdigit | isdecimal | isnumeric | chr |")
print(line)
for number in itertools.chain(range(1000), range(4969, 4978), range(8304, 11000)):
    char = chr(number)
    if (char.isdigit() or char.isdecimal() or char.isnumeric()):
        print('| {0:>6} | {1:^7} | {2:^9} | {3:^9} | {4:3} |'.format(
            number,
            '+' if char.isdigit() else '-',
            '+' if char.isdecimal() else '-',
            '+' if char.isnumeric() else '-',
            char
        )
    )

And the results

-------------------------------------------------
|       | isdigit | isdecimal | isnumeric | chr |
-------------------------------------------------
|     48 |    +    |     +     |     +     | 0   |
|     49 |    +    |     +     |     +     | 1   |
|     50 |    +    |     +     |     +     | 2   |
|     51 |    +    |     +     |     +     | 3   |
|     52 |    +    |     +     |     +     | 4   |
|     53 |    +    |     +     |     +     | 5   |
|     54 |    +    |     +     |     +     | 6   |
|     55 |    +    |     +     |     +     | 7   |
|     56 |    +    |     +     |     +     | 8   |
|     57 |    +    |     +     |     +     | 9   |
|    178 |    +    |     -     |     +     | ²   |
|    179 |    +    |     -     |     +     | ³   |
|    185 |    +    |     -     |     +     | ¹   |
|    188 |    -    |     -     |     +     | ¼   |
|    189 |    -    |     -     |     +     | ½   |
|    190 |    -    |     -     |     +     | ¾   |
|   4969 |    +    |     -     |     +     |    |
|   4970 |    +    |     -     |     +     |    |
|   4971 |    +    |     -     |     +     |    |
|   4972 |    +    |     -     |     +     |    |
|   4973 |    +    |     -     |     +     |    |
|   4974 |    +    |     -     |     +     |    |
|   4975 |    +    |     -     |     +     |    |
|   4976 |    +    |     -     |     +     |   |
|   4977 |    +    |     -     |     +     |    |
|   8304 |    +    |     -     |     +     |    |
|   8308 |    +    |     -     |     +     |    |
|   8309 |    +    |     -     |     +     |    |
|   8310 |    +    |     -     |     +     |    |
|   8311 |    +    |     -     |     +     |    |
|   8312 |    +    |     -     |     +     |    |
|   8313 |    +    |     -     |     +     |    |
|   8320 |    +    |     -     |     +     |    |
|   8321 |    +    |     -     |     +     |    |
|   8322 |    +    |     -     |     +     |    |
|   8323 |    +    |     -     |     +     |    |
|   8324 |    +    |     -     |     +     |    |
|   8325 |    +    |     -     |     +     |    |
|   8326 |    +    |     -     |     +     |    |
|   8327 |    +    |     -     |     +     |    |
|   8328 |    +    |     -     |     +     |    |
|   8329 |    +    |     -     |     +     |    |
|   8528 |    -    |     -     |     +     |    |
|   8529 |    -    |     -     |     +     |    |
|   8530 |    -    |     -     |     +     |    |
|   8531 |    -    |     -     |     +     |    |
|   8532 |    -    |     -     |     +     |    |
|   8533 |    -    |     -     |     +     |    |
|   8534 |    -    |     -     |     +     |    |
|   8535 |    -    |     -     |     +     |    |
|   8536 |    -    |     -     |     +     |    |
|   8537 |    -    |     -     |     +     |    |
|   8538 |    -    |     -     |     +     |    |
|   8539 |    -    |     -     |     +     |    |
|   8540 |    -    |     -     |     +     |    |
|   8541 |    -    |     -     |     +     |    |
|   8542 |    -    |     -     |     +     |    |
|   8543 |    -    |     -     |     +     |    |
|   8544 |    -    |     -     |     +     |    |
|   8545 |    -    |     -     |     +     |    |
|   8546 |    -    |     -     |     +     |    |
|   8547 |    -    |     -     |     +     |    |
|   8548 |    -    |     -     |     +     |    |
|   8549 |    -    |     -     |     +     |    |
|   8550 |    -    |     -     |     +     |    |
|   8551 |    -    |     -     |     +     |    |
|   8552 |    -    |     -     |     +     |    |
|   8553 |    -    |     -     |     +     |    |
|   8554 |    -    |     -     |     +     |    |
|   8555 |    -    |     -     |     +     |    |
|   8556 |    -    |     -     |     +     |    |
|   8557 |    -    |     -     |     +     |    |
|   8558 |    -    |     -     |     +     |    |
|   8559 |    -    |     -     |     +     |    |
|   8560 |    -    |     -     |     +     |    |
|   8561 |    -    |     -     |     +     |    |
|   8562 |    -    |     -     |     +     |    |
|   8563 |    -    |     -     |     +     |    |
|   8564 |    -    |     -     |     +     |    |
|   8565 |    -    |     -     |     +     |    |
|   8566 |    -    |     -     |     +     |    |
|   8567 |    -    |     -     |     +     |    |
|   8568 |    -    |     -     |     +     |    |
|   8569 |    -    |     -     |     +     |    |
|   8570 |    -    |     -     |     +     |    |
|   8571 |    -    |     -     |     +     |    |
|   8572 |    -    |     -     |     +     |    |
|   8573 |    -    |     -     |     +     |    |
|   8574 |    -    |     -     |     +     |    |
|   8575 |    -    |     -     |     +     |    |
|   8576 |    -    |     -     |     +     |    |
|   8577 |    -    |     -     |     +     |    |
|   8578 |    -    |     -     |     +     |    |
|   8581 |    -    |     -     |     +     |    |
|   8582 |    -    |     -     |     +     |    |
|   8583 |    -    |     -     |     +     |    |
|   8584 |    -    |     -     |     +     |    |
|   8585 |    -    |     -     |     +     |    |
|   9312 |    +    |     -     |     +     |    |
|   9313 |    +    |     -     |     +     |    |
|   9314 |    +    |     -     |     +     |    |
|   9315 |    +    |     -     |     +     |    |
|   9316 |    +    |     -     |     +     |    |
|   9317 |    +    |     -     |     +     |    |
|   9318 |    +    |     -     |     +     |    |
|   9319 |    +    |     -     |     +     |    |
|   9320 |    +    |     -     |     +     |    |
|   9321 |    -    |     -     |     +     |    |
|   9322 |    -    |     -     |     +     |    |
|   9323 |    -    |     -     |     +     |    |
|   9324 |    -    |     -     |     +     |    |
|   9325 |    -    |     -     |     +     |    |
|   9326 |    -    |     -     |     +     |    |
|   9327 |    -    |     -     |     +     |    |
|   9328 |    -    |     -     |     +     |    |
|   9329 |    -    |     -     |     +     |    |
|   9330 |    -    |     -     |     +     |    |
|   9331 |    -    |     -     |     +     |    |
|   9332 |    +    |     -     |     +     |    |
|   9333 |    +    |     -     |     +     |    |
|   9334 |    +    |     -     |     +     |    |
|   9335 |    +    |     -     |     +     |    |
|   9336 |    +    |     -     |     +     |    |
|   9337 |    +    |     -     |     +     |    |
|   9338 |    +    |     -     |     +     |    |
|   9339 |    +    |     -     |     +     |    |
|   9340 |    +    |     -     |     +     |    |
|   9341 |    -    |     -     |     +     |    |
|   9342 |    -    |     -     |     +     |    |
|   9343 |    -    |     -     |     +     |    |
|   9344 |    -    |     -     |     +     |    |
|   9345 |    -    |     -     |     +     |    |
|   9346 |    -    |     -     |     +     |    |
|   9347 |    -    |     -     |     +     |    |
|   9348 |    -    |     -     |     +     |    |
|   9349 |    -    |     -     |     +     |    |
|   9350 |    -    |     -     |     +     |    |
|   9351 |    -    |     -     |     +     |    |
|   9352 |    +    |     -     |     +     |    |
|   9353 |    +    |     -     |     +     |    |
|   9354 |    +    |     -     |     +     |    |
|   9355 |    +    |     -     |     +     |    |
|   9356 |    +    |     -     |     +     |    |
|   9357 |    +    |     -     |     +     |    |
|   9358 |    +    |     -     |     +     |    |
|   9359 |    +    |     -     |     +     |    |
|   9360 |    +    |     -     |     +     |    |
|   9361 |    -    |     -     |     +     |    |
|   9362 |    -    |     -     |     +     |    |
|   9363 |    -    |     -     |     +     |    |
|   9364 |    -    |     -     |     +     |    |
|   9365 |    -    |     -     |     +     |    |
|   9366 |    -    |     -     |     +     |    |
|   9367 |    -    |     -     |     +     |    |
|   9368 |    -    |     -     |     +     |    |
|   9369 |    -    |     -     |     +     |    |
|   9370 |    -    |     -     |     +     |    |
|   9371 |    -    |     -     |     +     |    |
|   9450 |    +    |     -     |     +     |    |
|   9451 |    -    |     -     |     +     |    |
|   9452 |    -    |     -     |     +     |    |
|   9453 |    -    |     -     |     +     |    |
|   9454 |    -    |     -     |     +     |    |
|   9455 |    -    |     -     |     +     |    |
|   9456 |    -    |     -     |     +     |    |
|   9457 |    -    |     -     |     +     |    |
|   9458 |    -    |     -     |     +     |    |
|   9459 |    -    |     -     |     +     |    |
|   9460 |    -    |     -     |     +     |    |
|   9461 |    +    |     -     |     +     |    |
|   9462 |    +    |     -     |     +     |    |
|   9463 |    +    |     -     |     +     |    |
|   9464 |    +    |     -     |     +     |    |
|   9465 |    +    |     -     |     +     |    |
|   9466 |    +    |     -     |     +     |    |
|   9467 |    +    |     -     |     +     |    |
|   9468 |    +    |     -     |     +     |    |
|   9469 |    +    |     -     |     +     |    |
|   9470 |    -    |     -     |     +     |    |
|   9471 |    +    |     -     |     +     |    |
|  10102 |    +    |     -     |     +     |    |
|  10103 |    +    |     -     |     +     |    |
|  10104 |    +    |     -     |     +     |    |
|  10105 |    +    |     -     |     +     |    |
|  10106 |    +    |     -     |     +     |    |
|  10107 |    +    |     -     |     +     |    |
|  10108 |    +    |     -     |     +     |    |
|  10109 |    +    |     -     |     +     |    |
|  10110 |    +    |     -     |     +     |    |
|  10111 |    -    |     -     |     +     |    |
|  10112 |    +    |     -     |     +     |    |
|  10113 |    +    |     -     |     +     |    |
|  10114 |    +    |     -     |     +     |    |
|  10115 |    +    |     -     |     +     |    |
|  10116 |    +    |     -     |     +     |    |
|  10117 |    +    |     -     |     +     |    |
|  10118 |    +    |     -     |     +     |    |
|  10119 |    +    |     -     |     +     |    |
|  10120 |    +    |     -     |     +     |    |
|  10121 |    -    |     -     |     +     |    |
|  10122 |    +    |     -     |     +     |    |
|  10123 |    +    |     -     |     +     |    |
|  10124 |    +    |     -     |     +     |    |
|  10125 |    +    |     -     |     +     |    |
|  10126 |    +    |     -     |     +     |    |
|  10127 |    +    |     -     |     +     |    |
|  10128 |    +    |     -     |     +     |    |
|  10129 |    +    |     -     |     +     |    |
|  10130 |    +    |     -     |     +     |    |
|  10131 |    -    |     -     |     +     |    |

PS: I am sorry for the wrong format.

Solution 5 - Python

enter image description here

examples of isdigit a = "123"

a.isdigit()

true

a = "1.234"

a.isdigit()

false

a ="2²"

a.isdigit()

b = "3₄"

b.isdigit()

The roman numerals, currency numerators and fractions (usually written using unicode) are considered numeric characters but not digits. The isdigit() returns False if the string contains these characters

Roman Numerals : Ⅰ, Ⅱ ,Ⅺ currency numerators : $,₹,€,£ fractions : ½,⅔,¼

c = "Ⅺ"

c.isdigit()

false

d = "₹"

d.isdigit()

false

e = "½"

e.isdigit()

false

some examples of isdecimal ()

a = "123"

a.isdecimal()

true b = "3₄"

b.isdecimal()

c = "Ⅺ"

c.isdecimal()

false d = "₹"

d.isdecimal()

false e = "½"

e.isdecimal()

false

The superscript and subscripts are considered digit characters but not decimals. If the string contains these characters (usually written using unicode), isdecimal() returns False. Similarly, roman numerals, currency numerators and fractions are considered numeric numbers (usually written using unicode) but not decimals. The isdecimal() also returns False in this case.

source link : https://www.programiz.com/python-programming/methods/string/isdecimal https://www.programiz.com/python-programming/methods/string/isdigit

if i am wrong then please correct me just a new learner

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhoenixView Question on Stackoverflow
Solution 1 - PythonHenry KeiterView Answer on Stackoverflow
Solution 2 - PythonN RandhawaView Answer on Stackoverflow
Solution 3 - PythonPADYMKOView Answer on Stackoverflow
Solution 4 - PythondanquinView Answer on Stackoverflow
Solution 5 - PythonAyush TalesaraView Answer on Stackoverflow