From a625d742230f1831befd82297abc7c6425d439ec Mon Sep 17 00:00:00 2001 From: Mamta Wardhani Date: Tue, 1 Jul 2025 18:43:27 +0530 Subject: [PATCH 1/5] [Edit]: Python `.isnumeric()` --- .../strings/terms/isnumeric/isnumeric.md | 138 +++++++++++++----- 1 file changed, 104 insertions(+), 34 deletions(-) diff --git a/content/python/concepts/strings/terms/isnumeric/isnumeric.md b/content/python/concepts/strings/terms/isnumeric/isnumeric.md index 961ca63231b..d8cd89874f4 100644 --- a/content/python/concepts/strings/terms/isnumeric/isnumeric.md +++ b/content/python/concepts/strings/terms/isnumeric/isnumeric.md @@ -1,64 +1,134 @@ --- Title: '.isnumeric()' -Description: 'Verifies that all the characters within the string variable are numeric.' +Description: 'Returns `True` if all characters in a string are numeric characters.' Subjects: - - 'Data Science' - 'Computer Science' + - 'Web Development' Tags: + - 'Booleans' - 'Methods' - - 'Functions' + - 'Strings' + - 'Validation' CatalogContent: - 'learn-python-3' - 'paths/computer-science' --- -**`.isnumeric()`** is used to verify that the string variable consists of numerical characters only. Which means the string cannot contain a space, comma, dash, or any other characters that are not numerical. +The **`.isnumeric()`** method is a built-in [string](https://www.codecademy.com/resources/docs/python/strings) method in Python that determines whether all characters in a string are numeric characters. This method returns a [Boolean](https://www.codecademy.com/resources/docs/general/data-types/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. -## Syntax +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. -There are two ways of using this function: +## Syntax ```pseudo -str.isnumeric(str_variable) - -str_variable.isnumeric() +string.isnumeric() ``` -## Example +**Parameters:** + +The `.isnumeric()` method does not take any parameters. + +**Return value:** + +- Returns `True` if all characters in the string are numeric characters and the string is not empty +- Returns `False` if the string contains any non-numeric characters, is empty, or contains whitespace + +## Example 1: Basic Usage -The following examples implement `.isnumeric()` and will return `True`: +This example demonstrates the fundamental use of the `.isnumeric()` method to check if a string contains only numeric characters: -```python -print(str.isnumeric("2")) -print(str.isnumeric("½")) -print(str.isnumeric("2023")) +```py +# Check if string contains only numeric characters +text = "123456" +result = text.isnumeric() +print(result) + +# Check with mixed content +mixed_text = "123abc" +mixed_result = mixed_text.isnumeric() +print(mixed_result) + +# Check with empty string +empty_text = "" +empty_result = empty_text.isnumeric() +print(empty_result) +``` + +This example results in the following output: + +```shell +True +False +False ``` -or +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 -```python -# Define the string variables -num = "2" -one_half = "½" -year = "2023" +This example shows how to use `.isnumeric()` for validating user input in a real-world scenario where only numeric input is acceptable: -# Show the results -print(num.isnumeric()) -print(one_half.isnumeric()) -print(year.isnumeric()) +```py +# Simulate user input validation for age +user_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") ``` -## Codebyte Example +This example results in the following output: + +```shell +Age 25: Valid adult +'thirty': Invalid input - not numeric +Age 18: Valid adult +'12.5': Invalid input - not numeric +Age 0: Valid minor +``` -This example is runnable and demonstrates instances where non-numeric values are passed into the `.isnumeric()` function: +This example demonstrates how `.isnumeric()` can be used to filter valid numeric input before converting to integers and performing age validation logic. -```codebyte/python -# A string that includes a space -print(str.isnumeric("20 21")) +## Codebyte Example: Data Processing -# A string that includes a comma -print(str.isnumeric("20,21")) +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: -# A string that includes a dash -print(str.isnumeric("-21")) +```codebyte/python +# Process mixed data to extract numeric values +data_list = ["100", "hello", "250", "world", "75", "test123", "500"] +numeric_values = [] +non_numeric_items = [] + +for item in data_list: + if item.isnumeric(): + numeric_values.append(int(item)) + else: + non_numeric_items.append(item) + +print("Numeric values:", numeric_values) +print("Non-numeric items:", non_numeric_items) +print("Sum of numeric values:", sum(numeric_values)) +print("Average:", sum(numeric_values) / len(numeric_values)) ``` + +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`. From 9e8eb359378274798a5709bd41bdb0c941dee53f Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 10 Jul 2025 22:22:08 +0530 Subject: [PATCH 2/5] Update content/python/concepts/strings/terms/isnumeric/isnumeric.md --- content/python/concepts/strings/terms/isnumeric/isnumeric.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/strings/terms/isnumeric/isnumeric.md b/content/python/concepts/strings/terms/isnumeric/isnumeric.md index d8cd89874f4..1431b3ce14f 100644 --- a/content/python/concepts/strings/terms/isnumeric/isnumeric.md +++ b/content/python/concepts/strings/terms/isnumeric/isnumeric.md @@ -1,6 +1,6 @@ --- Title: '.isnumeric()' -Description: 'Returns `True` if all characters in a string are numeric characters.' +Description: 'Returns True if all characters in a string are numeric characters.' Subjects: - 'Computer Science' - 'Web Development' From 51c510a0a45aad4726c287591adcac016bcd6a9a Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 10 Jul 2025 22:26:43 +0530 Subject: [PATCH 3/5] Update content/python/concepts/strings/terms/isnumeric/isnumeric.md --- content/python/concepts/strings/terms/isnumeric/isnumeric.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/strings/terms/isnumeric/isnumeric.md b/content/python/concepts/strings/terms/isnumeric/isnumeric.md index 1431b3ce14f..d5b90125e6d 100644 --- a/content/python/concepts/strings/terms/isnumeric/isnumeric.md +++ b/content/python/concepts/strings/terms/isnumeric/isnumeric.md @@ -62,7 +62,7 @@ False False ``` -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. +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 From cf4abdb0fcd79fae9d87f3f0a937d4ba144c67fa Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 10 Jul 2025 22:27:37 +0530 Subject: [PATCH 4/5] Update content/python/concepts/strings/terms/isnumeric/isnumeric.md --- content/python/concepts/strings/terms/isnumeric/isnumeric.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/strings/terms/isnumeric/isnumeric.md b/content/python/concepts/strings/terms/isnumeric/isnumeric.md index d5b90125e6d..b6efe6138d4 100644 --- a/content/python/concepts/strings/terms/isnumeric/isnumeric.md +++ b/content/python/concepts/strings/terms/isnumeric/isnumeric.md @@ -83,7 +83,7 @@ for user_input in user_inputs: print(f"'{user_input}': Invalid input - not numeric") ``` -This example results in the following output: +The output of the above code will be: ```shell Age 25: Valid adult From 47988495dd2da10c15d01619414510ab39146c84 Mon Sep 17 00:00:00 2001 From: Avdhoot <50920321+avdhoottt@users.noreply.github.com> Date: Thu, 10 Jul 2025 22:29:25 +0530 Subject: [PATCH 5/5] Update content/python/concepts/strings/terms/isnumeric/isnumeric.md --- content/python/concepts/strings/terms/isnumeric/isnumeric.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/content/python/concepts/strings/terms/isnumeric/isnumeric.md b/content/python/concepts/strings/terms/isnumeric/isnumeric.md index b6efe6138d4..8658eb6dac7 100644 --- a/content/python/concepts/strings/terms/isnumeric/isnumeric.md +++ b/content/python/concepts/strings/terms/isnumeric/isnumeric.md @@ -93,7 +93,7 @@ Age 18: Valid adult Age 0: Valid minor ``` -This example demonstrates how `.isnumeric()` can be used to filter valid numeric input before converting to integers and performing age validation logic. +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