What is the isdigit() function in Python?

Key takeaways:

  • The isdigit() method checks if a string contains only numeric characters or not.

  • It returns True if the string is entirely numeric and non-empty, otherwise False.

  • It does not recognize currency values, fractions, or Roman numerals as numeric digits.

  • The method is particularly useful for input validation, data filtering, and ensuring safe type conversions in Python.

The Python isdigit() method is a built-in string function used to check if a string consists solely of numeric characters. This method is especially helpful when validating input or ensuring that a string can be safely converted into a number. It returns a Boolean value, making it straightforward to determine whether a string meets these criteria.

High level diagram of isdigit() function
High level diagram of isdigit() function

Understanding the isdigit() function

The isdigit() function is a built-in string method in Python that checks whether all the characters in a string are digits. If every character in the string is a numeric digit (0–9), the method returns True; otherwise, it returns False.

Syntax

str.isdigit()

Return value of isdigit() function

  • If every character in the string is a numeric digit, the method returns True.

  • If any character in the string is not a numeric digit, the method returns False.

Key points

  1. Works only on strings: The method can only be called on string objects. Attempting to use it on non-string types will result in an error.

  2. Checks each character: The method checks every character in the string individually. If any character is non-numeric, the result will be False.

Examples of using isdigit()

Let’s look at some examples to understand how the isdigit() function works.

1. Basic use

Here’s how the isdigit() method behaves with numeric and non-numeric strings:

# Example of numeric strings
print("12345".isdigit()) # Output: True
# Example of non-numeric strings
print("123abc".isdigit()) # Output: False
print("12.34".isdigit()) # Output: False

2. Empty strings

Let’s see how the method behaves with an empty string:

# Empty strings always return False
print("".isdigit()) # Output: False

3. Unicode digits

The isdigit() method also supports Unicode characters categorized as digits. Here’s an example:

# Unicode numeric characters
print("²³".isdigit()) # Output: True
print("٣".isdigit()) # Output: True # Arabic numeral 3

Use cases for isdigit()

The isdigit() method can be used in a variety of real-world scenarios, such as:

1. Input validation using isdigit()

We can validate user input to ensure it consists only of numeric characters.

user_input = input("")
if user_input.isdigit():
print("Valid age!")
else:
print("Invalid input. Please enter numeric characters only.")

Enter the input below

2. Filtering numeric data using isdigit()

We can filter out strings that contain only numbers from a list of mixed data.

data = ["123", "abc", "45", "12a", "789"]
numeric_data = [item for item in data if item.isdigit()]
print(numeric_data) # Output: ['123', '45', '789']

3. Checking Unicode digits using isdigit()

In applications dealing with multilingual text, isdigit() can help identify numeric characters across different languages.

text = ["123", "٣٤٥", "abc"]
unicode_digits = [item for item in text if item.isdigit()]
print(unicode_digits) # Output: ['123', '٣٤٥']

Limitations of isdigit()

While the isdigit() method is powerful, it has some limitations:

  1. No decimal or negative numbers: The method does not consider decimal points (.) or negative signs (-) as part of a numeric string.

print("-123".isdigit()) # Output: False
print("12.3".isdigit()) # Output: False
  1. Special characters: Characters like spaces or symbols in the string will cause the method to return False.

  2. Special numeric values: The method does not recognize currency values, fractions, or Roman numerals as digits.

Learn the basics with our engaging course!

Start your coding journey with the “Learn Python” course—the perfect course for absolute beginners! Whether you’re exploring coding as a hobby or building a foundation for a tech career, this course is your gateway to mastering Python—the most beginner-friendly and in-demand programming language. With simple explanations, interactive exercises, and real-world examples, you’ll confidently write your first programs and understand Python essentials. Our step-by-step approach ensures you grasp core concepts while having fun along the way. Join now and start your Python journey today—no prior experience is required!

Conclusion

The isdigit() function is a useful tool in Python for verifying whether a string contains only numeric characters. It provides a reliable way to validate input or filter data, ensuring that non-numeric characters are identified and handled appropriately. We can effectively use this function in various applications by understanding its behavior and limitations, such as its inability to recognize currency symbols or fractions as digits.

Frequently asked questions

Haven’t found what you were looking for? Contact Us


How to check if a character is a number in Python?

The isdigit() method is a string method that returns True if all characters in the string are digits (0-9), otherwise False.


What is `isdigit()` and `isalpha()` in Python?

  • isdigit() method: Returns True if the string consists only of digits; otherwise, returns False.

  • isalpha() method: Returns True if the string contains only letters; otherwise, False.


What is the difference between `isdigit()` and `isalnum()` in Python?

  • isdigit() method: Checks for digits only (0-9).
  • isalnum() method: Checks for alphanumeric characters (letters a-z, A-Z and digits 0-9).

Can `isdigit()` recognize numbers in non-Latin scripts?

Yes, the isdigit() method in Python can recognize numbers in non-Latin scripts, as long as they are defined as numeric characters in Unicode.

Here’s why:

  • Unicode support: Python 3 fully supports Unicode, which includes a wide range of characters from various scripts.
  • isdigit() and unicode: The isdigit() method checks if a character is a numeric digit (0-9) or any other character that has the Unicode property “Nd” (Numeric, decimal digit). This means it will return True for digits in scripts like Devanagari (e.g., “१”), Arabic (e.g., “٩”), and others.

Example:

devanagari_digit = "१"
arabic_digit = "٩"

print(devanagari_digit.isdigit())  # Output: True
print(arabic_digit.isdigit())      # Output: True

While isdigit() can identify digits in different scripts, you might need to consider the overall context of the text and use other methods (like isnumeric()) if you need to handle other numeric characters like fractions or Roman numerals.


What is the `isdigit()` character function used for?

The isdigit() character function is used to determine if a string consists entirely of digits. It’s a handy tool in programming for tasks like:

  • Data validation: Checking if user input is a valid number (e.g., age, phone number, ID).
  • Parsing data: Extracting numerical information from a string.
  • Formatting output: Ensuring numbers are displayed correctly.

How it works:

The isdigit() function goes through each character in a string. If all characters are digits (0-9), it returns True. Otherwise, it returns False.


Free Resources

Copyright ©2025 Educative, Inc. All rights reserved