Python Forum
str.isdigit() vs str.isnumeric()
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
str.isdigit() vs str.isnumeric()
#1
can anyone summarize the differences between str.isdigit() and str.isnumeric() and maybe also str.isdecimal()? i have been checking numbers with try/expect around int() or float() in part because if i have success, i have the converted value.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
I agree that from documentation of str.isidigit(), str.isnumeric(), str.isdecimal() is hard to comprehend differences between these string methods.

Subtle differences include:

- str.isdigit() returns True if strings containing only the digits 0-9
- str.isnumeric() returns True if string contains only numeric characters (including 0-9 but also likes Japanese kanji one '一')
- str.isdecimal() returns True if string contains only decimal numbers (1² is not decimal number, but it's str.isdigit() and str.isnumeric())

>>> digits = '123'
>>> numerics = '一二三'   # '123'in Kanji
>>> decimals = '1²'
>>> digits.isdigit()                                                       
True
>>> numerics.isdigit()                                                     
False
>>> decimals.isdigit()                                                     
True
>>> digits.isnumeric()                                                     
True
>>> numerics.isnumeric()                                                   
True
>>> decimals.isnumeric()                                                   
True
>>> digits.isdecimal()                                                     
True
>>> numerics.isdecimal()                                                   
False
>>> decimals.isdecimal()                                                   
False
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
now i am wondering what was the intended purpose of these 3 different bethods.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
I think that digging in mail.python.org may give answer to that.

To add some confusion, str.isdecimal() returns True for decimal numbers in some(?) character systems.

>>> kanji = '一'               # Kanji 1                                            
>>> thai = '๑'                 # Thai digit 1                                            
>>> kanji.isdecimal()                                                      
False
>>> thai.isdecimal()                                                       
True
>>> int(thai)
1
>>> int(kanji)
/.../
ValueError: invalid literal for int() with base 10: '一'
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
and i am curious if any of them is an exact match to what int(str,0) accepts.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Filter and str.isdigit producing an error tester_V 5 1,853 Aug-12-2022, 07:50 AM
Last Post: Gribouillis
  Python isdigit() and None samtal 6 7,978 May-06-2021, 05:13 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020