Methods and Functions

Python String isnumeric()

In Python, there are built-in functions for practically any action that can be performed on a string. When working with strings in Python, you may wish to verify whether the strings contain only letters, only numbers, or a mixture of any alphanumeric characters. For example, a program that prompts the user to enter a username may want to ensure that the username contains numeric values too. To check for this condition, the Python isnumeric() string function is used.

Python isnumeric() function

Definition

  • The Python isnumeric() String function examines a string for numeric characters and returns True only if the string contains all numeric characters, i.e. numerals (0-9); else returns False.
  • Python isnumeric() method returns True if all of the characters in the string are numeric (only numbers). If not, it returns False.

Python isnumeric()

The Python isnumeric() function returns a Boolean value depending on the existence of numeric characters in the string. In Python, Numeric characters include decimal characters (such as 0, 1, 2…), digits (such as subscript, superscript), and characters with the Unicode numeric value feature (such as fractions, roman numerals, currency numerators).

We can write numeric characters in Unicode by simply prefixing the ‘u’ with other numeric characters.

Note – Python isnumeric() always returns False for floating-point numbers.

isnumeric() Syntax

The syntax followed by Python isnumeric() is as follows:

                    

string.isnumeric()

isnumeric() Parameters

Python String isnumeric() function does not accept any parameters.

Return value from isnumeric()

The isnumeric() is responsible for returning a Boolean value:

  • True – if all the characters are numeric in the string
  • False – if at least one character is not a numeric character and also if there exist whitespaces between them.

Note – If the string is empty, the isnumeric() function returns a False value.

Example 1: Working of isnumeric()

Example

                    

# Python program to illustrate isnumeric()
# contains all numbers
num = '12345'
print(num.isnumeric())

# contains fraction
num = '¾'
print(num.isnumeric())

# contains Unicode numerics
num = '\u0034'
print(num.isnumeric())

# contains Unicode numerics
num = '\u00BD'
print(num.isnumeric())

Output

                    

True
True
True
True

In the above example, the Python isnumeric() function returns True for all numeric numbers, Unicode for numeric and vulgar fraction numeric values such as 12345, ¾, ‘\u00BD’, ‘\u0034’, and so on.

Example 2: Working of isnumeric()

Example

                    

# Python program to illustrate isnumeric()
# contains whitespaces between numbers
num = '123 45'
print(num.isnumeric())

# contains alphabetic characters and symbols
num = 'Hello@*1234'
print(num.isnumeric())

# contains floating-point numbet
num = '10.632'
print(num.isnumeric())

# contains empty string
num = ' '
print(num.isnumeric())

Output

                    

False
False
False
False

Example 3: How to use isnumeric()?

Few real life applications of Python isnumeric() function are:

Example

                    

string = '\u00B23455'

if string.isnumeric() == True:
    print('All characters are numeric')
else:
    print('All characters are numeric')

Output

                    

All characters are numeric

Example 4: isnumeric() additional features

The Python isnumeric() function can also handle numeric values of many national languages. In Chinese, for example, the number 1,2,3 is written as 一,二,三. For the following numeric characters, isnumeric() returns True:

                    

val = '一'
print(val.isnumeric())

val = '二'
print(val.isnumeric())

val = '三'
print(val.isnumeric())

Output

                    

True
True
True

Frequently Asked Questions

Q1. What does isnumeric mean in Python?

When working with strings in Python, you may wish to verify whether the strings contain only letters, only numbers, or a mixture of any alphanumeric characters. For example, a program that prompts the user to enter a username may want to ensure that the username contains numeric values too. To check for this condition, the Python isnumeric() string function is used.

The Python isnumeric() String function examines a string for numeric characters and returns True only if the string contains all numeric characters, i.e. numerals (0-9); else returns False.

Q2. What is the difference between isdigit and isnumeric in Python?

  • Python isdigit() – If all of the characters in a string are digits, the Python isdigit() method returns True. If it does not, it returns False. The Python String isdigit() method is a built-in method for string manipulation and handling.
  • Python isnumeric() – The Python isnumeric() String function examines a string for numeric characters and returns True only if the string contains all numeric characters, i.e. numerals (0-9); else returns False.

Example

                    

val = '50'
print(val.isdigit())
print(val.isnumeric())
print('')

# u00b2 is Superscript two
val = '\u00b2'
print(val.isdigit())
print(val.isnumeric())
print('')

val = 'â…“'
print(val.isdigit())
print(val.isnumeric())

Output

                    

True
True

True
True

False
True

The main difference between both the functions is:

  • The isdigit() method accepts only decimals, subscripts, and superscripts.
  • The isnumeric() function supports Digits, Vulgar Fractions, Subscripts, Superscripts, Roman Numerals, and Currency Numerators.

Q3. How do you check if a string contains all numbers in Python?

To check if the string contains numeric values, we implement the Python String isnumeric() function. Python isnumeric() method returns True if all of the characters in the string are numeric (only numbers). If not, it returns False.

The Python isnumeric() function returns a Boolean value depending on the existence of numeric characters in the string. In Python, Numeric characters include decimal characters (such as 0, 1, 2…), digits (such as subscript, superscript), and characters with the Unicode numeric value feature (such as fractions, roman numerals, currency numerators).

Example

                    

val = '5005'
print(val.isnumeric())

val = '10534875'
print(val.isnumeric())

val = 'python15353'
print(val.isnumeric())

val = '20.452'
print(val.isnumeric())

val = ' '
print(val.isnumeric())

Output

                    

True
True
False
False
False

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

Your email address will not be published. Required fields are marked *

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.