The difference between Str function isdigit, isdecimal and IsNumeric in Python

Source: Internet
Author: User


num = "1" #unicode
Num.isdigit () # True
Num.isdecimal () # True
Num.isnumeric () # True

num = "1" # Full width
Num.isdigit () # True
Num.isdecimal () # True
Num.isnumeric () # True

num = b "1" # byte
Num.isdigit () # True
Num.isdecimal () # attributeerror ' bytes ' object has no attribute ' isdecimal '
Num.isnumeric () # attributeerror ' bytes ' object has no attribute ' IsNumeric '

num = "IV" # Roman numerals
Num.isdigit () # True
Num.isdecimal () # False
Num.isnumeric () # True

num = "Four" # Kanji
Num.isdigit () # False
Num.isdecimal () # False
Num.isnumeric () # True

===================
isdigit ()
true:unicode number, byte digit (single byte), full-width digit (double byte), Roman numerals
False: Chinese numerals
Error: None

Isdecimal ()
true:unicode number, full-width digit (double-byte)
False: Roman numerals, Chinese numerals
error:byte number (single byte)

IsNumeric ()
true:unicode numbers, full-width numerals (double-byte), Roman numerals, Chinese numerals
False: None
error:byte number (single byte)

================
Import Unicodedata

Unicodedata.digit ("2") # 2
Unicodedata.decimal ("2") # 2
Unicodedata.numeric ("2") # 2.0

Unicodedata.digit ("2") # 2
Unicodedata.decimal ("2") # 2
Unicodedata.numeric ("2") # 2.0

Unicodedata.digit (b "3") # Typeerror:must is str, not bytes
Unicodedata.decimal (b "3") # Typeerror:must is str, not bytes
Unicodedata.numeric (b "3") # Typeerror:must is str, not bytes

Unicodedata.digit ("Ⅷ") # Valueerror:not a digit
Unicodedata.decimal ("Ⅷ") # Valueerror:not a decimal
Unicodedata.numeric ("Ⅷ") # 8.0

Unicodedata.digit ("four") # Valueerror:not a digit
Unicodedata.decimal ("four") # Valueerror:not a decimal
Unicodedata.numeric ("four") # 4.0

# "0", "0", "one", "Tearoom", "Two", "弐", "three", "ginseng", "four", "five", "six", "seven", "eight", "Nine", "Ten", "20", "30", "registered", "Hundred", "thousand", "million", "million", "Million"

The difference between Str function isdigit, isdecimal and IsNumeric in Python

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.