Python string isnumeric() method

Python String isnumeric() Method

Description

The isnumeric() method checks whether a string consists only of numeric characters. This method only works on Unicode objects.

Note: Unlike Python 2, all strings in Python 3 are represented in Unicode. Therefore, strings may contain either Unicode characters (literal representation of 3/4) or their Unicode values (u00BE). Digits (0–9), exponents (2^2, 10^6), fractions (3/4, 6/7), and the Unicode value for a number (u0030) are all considered numeric.

Syntax

The syntax of the isnumeric() method is as follows:

var.isnumeric()

Parameters

NA

Return Value

This method returns true if all characters in the string are numeric; otherwise, it returns false.

Example

The following example demonstrates the use of the isnumeric() method.

var = "u0031u0030"
var1 = var.isnumeric()
print ("Original string:", var)
print ("Is it only numbers?:", var1)
var = "3/4"
var2 = var.isnumeric()
print ("Original string:", var)
print ("Is it only numbers?:", var2)
var = "u0024100"
var3 = var.isnumeric()
print ("Original string:", var)
print ("Is it only numbers?:", var3)

Unicode 0030 represents ASCII 0, and 0031 represents ASCII 1.

When you run this program, it will produce the following output −

Original string: 10
Is it only digits? : True
Original string: 3/4
Is it only digits? : True
Original string: $100
Is it only digits? : False

Leave a Reply

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