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 a 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.

Syntax

The syntax of isdigit() method is:

string.isdigit()

Parameters

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

Return value

The isdigit() method returns:

  • True if all characters in the string contain digits
  • False if the string has at least one character that 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 a valid digit

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 a 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

Subscribe to get notified of the latest articles. We will never spam you. Be a part of our ever-growing community.

You May Also Like
Numpy.average() Function

numpy.average() Function

Table of Contents Hide SyntaxParametersReturn ValueRaisesExample 1: NumPy average() of Array values Example 2: NumPy average() of an array values in column-wise Example 3: NumPy average() of an array values in row-wiseExample…
View Post
Python Comment Block / Python Multiline Comment

Python Comment Block

Table of Contents Hide Introduction to Python Comment BlockTypes of comments in PythonSingle-line commentsInline commentsMultiline commentsUsing Multiple Hashtags (#)Python docstringsSingle line docstringsMulti-line docstrings Comments are a piece of text in…
View Post
Python Jsonpath

Python JSONPath

Table of Contents Hide JSONPath Library in PythonInstalling jsonpath-ng ModuleJsonpath operators:Parsing a Simple JSON Data using JSONPathParsing a Json Array using JSONPath Expression JSONPath is an expression language that is…
View Post