Home » Python

Python String | isdigit() Method with Example



Python String isdigit() method: In this tutorial, we will learn about isdigit() method of String in Python with Example.
Submitted by IncludeHelp, on July 08, 2018

isdigit() is an in-built method in Python, which is used to check whether a string contains only digits or not.

Digit value contains all decimal characters and other digits which may represent power or anything related to this, but it should be a complete digit, like "3".

This method is different from isdecimal() method and isnumeric() method because first method checks only decimal characters and second method checks numeric values including decimal characters. And this (isdigit()) method will consider only digit.

Note:

  • Digits value contains decimal characters and other Unicode digits, fractional part of the value like ½, ¼ etc are not considered as digit.
  • String should be Unicode object - to define a string as Unicode object, we use u as prefix of the string value.

Syntax:

    String.isdigit();

Parameter: None

Return type:

  • true - If all characters of the string are digits then method returns true.
  • false - If any of the characters of the string is not a digit then method returns false.

Example/program:

# Only digits (decimal characters)
str1 = u"362436"
print (str1.isdigit())

# digit value (no decimals but a digit)
str2 = u"3"
print (str2.isdigit())

# numeric values but not any digit
str3 = u"½¼"
print (str3.isdigit())

#digits, alphabets
str4 = u"Hello3624"
print (str4.isdigit())

Output

    True
    True
    True
    False

Reference: String isdigit()






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.