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. Required fields are marked *

Sign Up for Our Newsletters

Get notified of the best deals on our WordPress themes.

You May Also Like
List of Python free ebooks

List of Free Python Books

Table of Contents Hide Python SuccinctlyPython Data Science HandbookThink Python 2nd EditionAutomate the Boring Stuff with PythonMaking Games with Python & PygameData Structures and Algorithms in Python Google Python Style GuideA…
View Post