×


Python isnumeric() Method - Explained with Examples



The isnumeric() method is built-in Python allows handling string. This method will return True if all the characters in the string are numbers, if the characters in the string are not numbers it will return False. It can check integers, fractions, subscripts, an so on.

Here at LinuxAPT, we shall look into how to use the isnumeric() method in Python.


More about the isnumeric() Method

The isnumeric() method will return True if all characters in the string are numeric.

"-2" or "3.5" will return False because the characters "-", "." are not numeric.

It's syntax is given below:

string.isnumeric()

It gives No parameter.


Examples of using isnumeric() Method

1. have a look at the below function:

str = "874534"
x = str.isnumeric()
print(x)

It will give the below Output:

True


2. Also, consider the below function:

str = "-2"
x = str.isnumeric()
print(x)

It gives the below Output:

False


3. Another example is given below:

str1 = "-1"
print(str1.isnumeric())
str2 = "7347"
print(str2.isnumeric())
str3 = "83jj38"
print(str3.isnumeric())

It gives the below output:

False
True
False


4. Combining input() function:

print("Enter the string:")
x = input().isnumeric()
print(x)

The Output gives:

Enter the string: 32324
True


5. See this last isnumeric() function:

#s = '²3455'
s = '\u00B23455'
if s.isnumeric() == True:
  print('All characters are numeric.')
else:
  print('All characters are not numeric.')

The Output will give:

All characters are numeric.


[Need help in fixing Python function issues ? We can help you. ]


Conclusion

This article covers how to use the isnumeric() method in Python. In fact, the isnumeric() method returns True if all characters in a string are numeric characters. If not, it returns False.

A numeric character has following properties:

  • Numeric_Type=Decimal
  • Numeric_Type=Digit
  • Numeric_Type=Numeric

More Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on IbmiMedia.com

Also for Tech related tips, Visit forum.outsourcepath.com or General Technical tips on www.outsourcepath.com