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()




Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.