Python .isnumeric()
The .isnumeric() method is a built-in string method in Python that determines whether all characters in a string are numeric characters. This method returns a Boolean value indicating if the string consists entirely of numeric characters and is not empty. It is commonly used for input validation, data processing, and string analysis tasks where numeric content verification is required.
The .isnumeric() method provides a reliable way to check if a string contains only numeric characters, including digits (0-9), subscripts, superscripts, fractions, and other Unicode numeric characters. It is particularly useful in scenarios such as validating user input, processing CSV data, or filtering numeric content from mixed datasets.
Syntax
string.isnumeric()
Parameters:
The .isnumeric() method does not take any parameters.
Return value:
- Returns
Trueif all characters in the string are numeric characters and the string is not empty - Returns
Falseif the string contains any non-numeric characters, is empty, or contains whitespace
Example 1: Basic Usage
This example demonstrates the fundamental use of the .isnumeric() method to check if a string contains only numeric characters:
# Check if string contains only numeric characterstext = "123456"result = text.isnumeric()print(result)# Check with mixed contentmixed_text = "123abc"mixed_result = mixed_text.isnumeric()print(mixed_result)# Check with empty stringempty_text = ""empty_result = empty_text.isnumeric()print(empty_result)
This example results in the following output:
TrueFalseFalse
In the above code, the first string returns True because it contains only numeric digits. The second returns False because it contains alphabetic characters, and the third returns False because empty strings are not considered numeric.
Example 2: Input Validation
This example shows how to use .isnumeric() for validating user input in a real-world scenario where only numeric input is acceptable:
# Simulate user input validation for ageuser_inputs = ["25", "thirty", "18", "12.5", "0"]for user_input in user_inputs:if user_input.isnumeric():age = int(user_input)if age >= 18:print(f"Age {age}: Valid adult")else:print(f"Age {age}: Valid minor")else:print(f"'{user_input}': Invalid input - not numeric")
The output of the above code will be:
Age 25: Valid adult'thirty': Invalid input - not numericAge 18: Valid adult'12.5': Invalid input - not numericAge 0: Valid minor
The above example demonstrates how .isnumeric() can be used to filter valid numeric input before converting to integers and performing age validation logic.
Codebyte Example: Data Processing
This example illustrates using .isnumeric() to process and filter numeric data from a mixed dataset, which is common when working with CSV files or user-generated content:
This demonstrates a practical application of .isnumeric() for data cleaning and processing, separating numeric data for mathematical operations while identifying non-numeric entries for further handling.
Frequently Asked Questions
1. What’s the difference between .isnumeric(), .isdigit(), and .isdecimal()?
.isnumeric() accepts the widest range of numeric characters including Unicode numerals, superscripts, and fractions. .isdigit() accepts digits and superscripts but not fractions. .isdecimal() only accepts standard decimal digits (0-9).
2. Does .isnumeric() work with negative numbers or decimals?
No, .isnumeric() returns False for strings containing minus signs (-) or decimal points (.). It only recognizes pure numeric characters without mathematical symbols.
3. Why does .isnumeric() return False for empty strings?
An empty string is not considered to contain numeric characters, so .isnumeric() returns False following Python’s convention that validation methods require actual content to return True.
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python on Codecademy
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.
- With Certificate
- Beginner Friendly.24 hours