Python String isnumeric() Method



The python string isnumeric() method is used to check whether the string consists of numeric characters. This method returns true if all the characters in the input string are numeric and there is atleast one character. Otherwise, it returns false.

The numeric characters include digit characters, and all characters that have the Unicode numeric value property, e.g. U+2155, VULGAR FRACTION ONE FIFTH. Formally, numeric characters are those with the property value Numeric_Type=Digit, Numeric_Type=Decimal or Numeric_Type=Numeric.

In the following section, let us look into this method with more details.

Syntax

Following is the syntax for the python string isnumeric() method −

str.isnumeric()

Parameters

The python string isnumeric() method doesnot contain any parameters.

Return Value

The python string isnumeric() method returns true if all characters in the string are numeric and there is at least one character and false otherwise.

Example

The following is an example of the python string isnumeric() method. In this program, we are trying to find whether the string "Welcome2023" is alpha numeric.

#!/usr/bin/python
str = "Welcome2023"
result=str.isnumeric()
print("Are all the characters of the string numeric?", result)

On executing the above program, the following output is generated -

Are all the characters of the string numeric? False

Example

let us see another example -

#!/usr/bin/python
str = "2023"
result=str.isnumeric()
print("Are all the characters of the string numeric?", result)

The following is the output obtained by executing the above program -

Are all the characters of the string numeric? True

Example

The unicode representation of numbers are also considered as numeric by the isnumeric() method.

#!/usr/bin/python
str = "\u0030" #unicode for 0
result=str.isnumeric()
print("Are all the characters of the string numeric?", result)

The following output is obtained by executing the above program -

Are all the characters of the string numeric? True

Example

The python string isnumeric() method considers unicode representation of fractions as numeric only.

#!/usr/bin/python
str = "\u00BE" #unicode for fraction 3/4
result=str.isnumeric()
print("Are all the characters of the string numeric?", result)

The above program, on executing, displays the following output -

Are all the characters of the string numeric? True

Example

The Kharosthi numbers such as 𐩀,𐩂,𐩃,𐩄,𐩅,𐩆,𐩇 are considered as numeric. So, the python string isnumeric() method returns true.

#!/usr/bin/python
str = "𐩁𐩂𐩃" 
result=str.isnumeric()
print("Are all the characters of the string numeric?", result)

The output of the above program is displayed as follows -

Are all the characters of the string numeric? True
python_strings.htm
Advertisements