Home » Python

Python | Difference between string isdecimal(), isdigit(), isnumeric() and Methods



In this article, we will learn what is the difference between String’s isdecimal(), isnumeric() and isdigit() Method in Python programming language?
Submitted by IncludeHelp, on July 08, 2018

The methods isdigit(), isnumeric() and isdecimal() are in-built methods of String in python programming language, which are worked with strings as Unicode objects. These functions return either true or false.

The comparison is based on Unicode classifications,

isdecimal() isdigit() isnumeric()
Example of string with decimal characters:
"12345"
"12"
"98201"
Example of string with digits:
"12345"
"1233"
"3"
Example of string with numerics:
"12345"
"½¼"
"½"
"12345½"
Returns ‘true’ if all characters of the string are decimal. Returns ‘true’ if all characters of the string are digits. Returns ‘true if all characters of the string are numeric.
Read more:
String isdecimal() Method
Read more:
String isdigit() Method
Read more:
String isnumeric() Method

Example:

str1 = u"362436"    #decimal characters
str2 = u"3"         #unicode digit
str3 = u"½¼"        #fractional value

print "str1 :"
print "str1.isdecimal () : ", str1.isdecimal ()
print "str1.isnumeric () : ", str1.isnumeric ()
print "str1.isdigit () : ", str1.isdigit ()

print "str2 :"
print "str2.isdecimal () : ", str2.isdecimal ()
print "str2.isnumeric () : ", str2.isnumeric ()
print "str2.isdigit () : ", str2.isdigit ()

print "str3 :"
print "str3.isdecimal () : ", str3.isdecimal ()
print "str3.isnumeric () : ", str3.isnumeric ()
print "str3.isdigit () : ", str3.isdigit ()

Output

    str1 :
    str1.isdecimal () :  True
    str1.isnumeric () :  True
    str1.isdigit () :  True

    str2 :
    str2.isdecimal () :  True
    str2.isnumeric () :  True
    str2.isdigit () :  True

    str3 :
    str3.isdecimal () :  False
    str3.isnumeric () :  True
    str3.isdigit () :  False





Quick links:
C FAQ(s) C Advance programs C/C++ Tips & Tricks Puzzles JavaScript CSS Python Linux Commands PHP Android Articles More...

Was this page helpful? Please share with your friends...

Are you a blogger? Join our Blogging forum.



Comments and Discussions





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com (2015-2018), Some rights reserved.