str.isdigit() vs str.isnumeric() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: str.isdigit() vs str.isnumeric() (/thread-19092.html) |
str.isdigit() vs str.isnumeric() - Skaperen - Jun-13-2019 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. RE: str.isdigit() vs str.isnumeric() - perfringo - Jun-13-2019 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 RE: str.isdigit() vs str.isnumeric() - Skaperen - Jun-13-2019 now i am wondering what was the intended purpose of these 3 different bethods. RE: str.isdigit() vs str.isnumeric() - perfringo - Jun-13-2019 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: '一' RE: str.isdigit() vs str.isnumeric() - Skaperen - Jun-14-2019 and i am curious if any of them is an exact match to what int(str,0) accepts. |