The isdigit()
method is a string method that returns True
if all characters in the string are digits (0-9), otherwise False.
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, otherwiseFalse
.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.
str.isdigit()
isdigit()
functionIf 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
.
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.
Checks each character: The method checks every character in the string individually. If any character is non-numeric, the result will be False
.
isdigit()
Let’s look at some examples to understand how the isdigit()
function works.
Here’s how the isdigit()
method behaves with numeric and non-numeric strings:
# Example of numeric stringsprint("12345".isdigit()) # Output: True# Example of non-numeric stringsprint("123abc".isdigit()) # Output: Falseprint("12.34".isdigit()) # Output: False
Let’s see how the method behaves with an empty string:
# Empty strings always return Falseprint("".isdigit()) # Output: False
The isdigit()
method also supports Unicode characters categorized as digits. Here’s an example:
# Unicode numeric charactersprint("²³".isdigit()) # Output: Trueprint("٣".isdigit()) # Output: True # Arabic numeral 3
isdigit()
The isdigit()
method can be used in a variety of real-world scenarios, such as:
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
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']
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', '٣٤٥']
isdigit()
While the isdigit()
method is powerful, it has some limitations:
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: Falseprint("12.3".isdigit()) # Output: False
Special characters: Characters like spaces or symbols in the string will cause the method to return False
.
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!
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.
Haven’t found what you were looking for? Contact Us
Free Resources