Python String isdigit()

Python string isdigit() method is a built-in function that returns True if all the characters in a string are digits. Otherwise, it returns false.

What are the valid digits in Python?

In Python, a digit is a character that has the property value.

  • Numeric_Type=Digit 
  • Numeric_Type=Decimal.

The superscript and subscript (usually written using Unicode characters) are also considered digit characters. So any string containing these characters along with decimal characters is treated as a valid digit in Python.

The mentioned Unicode characters should have a numerical output. If the Unicode values do not return a valid digit, the isdigit() method returns false.

Note: The roman numerals, currency numerators and fractions (usually written using Unicode Characters) are considered numeric characters and not digits. The isdigit() returns will return False if the string contains roman characters, fractions or currency numerators.

This covers digits which cannot be used to form numbers in base 10, like the Kharosthi numbers.

isdigit() Syntax

The syntax of isdigit() method is:

string.isdigit()

isdigit() Parameters

The isdigit() method doesn’t take any parameters.

 isdigit() Return value

The isdigit() method returns:

  • True if all characters in the string contains digits
  • False if the string has at least one character is non-digit 

Example 1: Working with Python string isdigit() method

# Valid digit, returns true
text1 = "12345"
print(text1.isdigit())

# float, returns false
text2= "12.45"
print(text2.isdigit())

# alphanumeric, returns false
text3= "A123BC "
print(text3.isdigit())

# unicode for 0, returns true
text4="\u0030"
print(text4.isdigit())

# fraction, returns false
text5="½"
print(text5.isdigit())

Output

True
False
False
True
False

Example 2: How do I check if a string is Isdigit?

We can use Python string methods to check if all the characters in a string are digits. One of the popular methods is the Python string isdigit() method. In the below program, the isdigit() method returns true if the sequence of characters in a string is valid digit else returns false.

Based on the Boolean value we can evaluate and print if the given string is a valid digit or not.

# valid digit
text1 = "12345"
if(text1.isdigit()):
    print("The given string is valid digit")
else:
    print("The given string has non digit characters")

# \u00BD is fraction ½
text2= "\u00BD"
if(text2.isdigit()):
    print("The given string is valid digit")
else:
    print("The given string has non digit characters")

Output

The given string is valid digit
The given string has non digit characters
Leave a Reply

Your email address will not be published.

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
Python Trim String Strip() Vs Rstrip() Vs Lstrip()

Python Trim String – rstrip(), lstrip(), strip()

Table of Contents Hide Difference between strip() vs rstrip() vs lstrip()strip()Example of strip() function  rstrip() Example of rstrip() function   lstrip() Example of lstrip() function   Python provides three methods to trim the…
View Post
Calculate Euclidean Distance In Python

Calculate Euclidean Distance in Python

Table of Contents Hide How to Calculate Euclidean Distance in Python?Method 1: Using linalg.norm() Method in NumPyMethod 2: Using dot() and sqrt() methodsMethod 3: Using square() and sum() methodsMethod 4:…
View Post
Numpy.argmax() In Python

numpy.argmax() in Python

Table of Contents Hide SyntaxParametersReturn ValueFinding the maximum element from a matrix with Python numpy.argmax()Using np.unravel_index on argmax outputFinding Maximum Elements along columns using Python numpy.argmax() The numpy.argmax() function returns the indices…
View Post
Importerror No Module Named Requests

ImportError: No module named requests

In Python, if you try to import Requests without installing the module using pip, you will get ImportError: no module named requests error.  In this tutorial, let’s look at installing…
View Post