isnumeric() Python Method | Python String isnumeric()
Overview
The isnumeric python method is invoked on a string to check whether all the characters of the string are numeric (string containing numbers (0-9), Unicode numeric values, exponents) or not.
Syntax of isnumeric() Method in Python
The syntax of the isnumeric python method is very simple. It does not take any parameters. We need to call or invoke the isnumeric python method on a string.
Here, string is an immutable sequence data type invoking the isnumeric() python method. For example: 123
Parameters of isnumeric() Method in Python
The isnumeric python method is called from a string. The isnumeric python method does not take any parameter. The isnumeric python method checks whether the method invoking string is numeric or not.
Return Values of isnumeric() Method in Python
The isnumeric python method returns the boolean value after checking whether the string is either alphabets or numbers.
Note:
Numeric means the characters of the string can contain numbers, exponents, fractions, and Unicode.
-
True The isnumeric python method returns True if the entire string is numeric (contain numbers/ fraction/ unicode/ exponents).
-
False The isnumeric python method returns False if one or more characters are non numeric (contain character(s) other than numbers/ fraction/ unicode/ exponents).
Exceptions of isnumeric() Method in Python
Usually, the isnumeric python method does not raise an error if we use the correct syntax.
Even if the string calling the isnumeric() python method is numeric but contains white spaces (or empty spaces), the method returns False.
Check the example section below for a better understanding.
Example
Let us take some examples to understand the isnumeric() python method.
- When the calling string does not contain spaces:
Output:
- When the calling string contains spaces:
Output:
What is string.isnumeric() in Python?
The isnumeric python method is a built-in string handling method used to check whether all the string characters are numeric or not.
Note:
Numeric means the characters of the string can contain:
- Numbers (0 - 9).
- Exponents (2^2, 10^6, etc.).
- Fractions (3/4, 6/7, etc.).
- Unicode (\u0030).
Numeric does not consists of decimal numbers (.) and negative numbers (-).
For example, a = '2.36' or b = '-27' are non numeric in nature.
The isnumeric python method returns a boolean value. If all the characters of the calling string are numeric (containing numbers or fractions or Unicode or exponents), the isnumeric() method returns True. On the other hand, if the calling string contains characters other than numbers, fractions, Unicode, or exponents, then the isnumeric() method returns False.
Example:
- If the string is 12345, the isnumeric() method return True.
- If the string is \u00BD or \u0030, the isnumeric() method return True.
- If the string is 2.3 or 3 47, the isnumeric() method returns False.
Note:
If the string calling the isnumeric() python method is numeric but contains white spaces (or empty spaces), the method returns False.
Examples of isnumeric() Method in Python
So far, we have learned all about the isnumeric python method. Now, let us take some examples and understand the method better.
Example 1: Working of isnumeric()
The isnumeric in python returns a boolean value. If all the characters of the calling string are numeric (a string containing numbers or fractions or Unicode or exponents), the isnumeric() method returns True, else returns False.
Output:
Example 2: How to use isnumeric()
We can invoke the isnumeric python method on a string. The isnumeric in python return True if the calling string is numeric. On the other hand, if the calling string contains characters other than numbers or fractions or Unicode or exponents like a-z or A-Z or special symbols, then the isnumeric() method returns False.
Output:
Conclusion
- The isnumeric python method is used to check whether all the characters of the string are numeric (a string containing numbers or fractions or Unicode or exponents) or not.
- The syntax of the isnumeric in python is: string. isnumeric(). The isnumeric in python is called from a string. It does not take any parameters.
- The isnumeric in python returns the boolean value after checking the string is numeric or not. The isnumeric in python returns True if the entire string is numeric. Else, it returns False.
- If a string invoking the isnumeric() method is numeric (for example, 12 34) but contains white spaces (or empty spaces) then the method returns False.