Yandex

Python String isnumeric() Method – Check for Numeric Characters


Python String isnumeric() Method

The isnumeric() method in Python checks whether all characters in a string are numeric. It returns True if all characters are numbers, and False otherwise.

Syntax

string.isnumeric()

Parameters:

  • No parameters are required.

Returns:

  • True – if all characters in the string are numeric
  • False – if at least one character is not numeric

Example 1: Basic Usage

text = "12345"
print(text.isnumeric())
True

Example 2: String with Letters

text = "123abc"
print(text.isnumeric())
False

Example 3: Unicode Numeric Characters

text = "²³"
print(text.isnumeric())
True

These are superscript characters which are still considered numeric in Unicode.

Use Cases

  • Validate if a string is a numeric ID or code
  • Input validation for number-only fields
  • Useful in form handling and data cleaning

Difference Between isnumeric(), isdigit(), and isdecimal()

Method Supports Digits Supports Unicode Numbers Supports Decimal Characters
isnumeric() (e.g., ½, ²)
isdigit() (less than isnumeric)
isdecimal() (only 0–9)

Common Mistakes

  • Using isnumeric() on strings with spaces or punctuation returns False
  • Strings with decimal points like "3.14" are not numeric by this method

Interview Tip

isnumeric() is often useful when filtering user inputs or validating data in string format.

Summary

  • isnumeric() returns True if all characters in the string are numeric.
  • Supports Unicode digits and numeric characters like superscripts and fractions.
  • Does not recognize decimal points or negative signs as numeric.

Practice Problem

Write a program that asks the user to enter a student roll number and verifies if it's numeric:

roll = input("Enter student roll number: ")
if roll.isnumeric():
    print("Valid roll number.")
else:
    print("Invalid! Roll number must be numeric.")


Welcome to ProgramGuru

Sign up to start your journey with us

Support ProgramGuru.org

You can support this website with a contribution of your choice.

When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.

PayPal

UPI

PhonePe QR

MALLIKARJUNA M