Python 3 – String isnumeric() Method
In Python, the isnumeric()
method is used to check whether a string is numeric or not. The method returns True
if all the characters in the string are numeric, otherwise False
.
Syntax
string.isnumeric()
Parameters
The isnumeric()
method does not take any parameters.
Return Value
The isnumeric()
method returns a boolean value indicating whether the string is numeric or not.
Examples
Here are some examples that demonstrate the use of isnumeric()
method:
# Example 1
string = "123456"
print(string.isnumeric()) # True
# Example 2
string = "123 456"
print(string.isnumeric()) # False
# Example 3
string = "1.23"
print(string.isnumeric()) # False
In Example 1, the isnumeric()
method returns True
because all the characters in the string are numeric.
In Example 2, the isnumeric()
method returns False
because the string contains a whitespace character.
In Example 3, the isnumeric()
method returns False
because the string contains a decimal point.
Using isnumeric()
with Unicode Characters
The isnumeric()
method also works with Unicode characters that are classified as numeric. For instance, the Unicode character ‘½’ (U+00BD) is a numeric character, and the isnumeric()
method will return True
if it is present in the string.
# Example 4
string = "½"
print(string.isnumeric()) # True
Labeling Code
# Example 1
string = "123456"
print(string.isnumeric()) # True
# Example 2
string = "123 456"
print(string.isnumeric()) # False
# Example 3
string = "1.23"
print(string.isnumeric()) # False
# Example 4
string = "½"
print(string.isnumeric()) # True
The above code has been labeled as Python, as it is written in Python programming language.
Conclusion
In conclusion, the isnumeric()
method is a useful way of checking whether a string is numeric or not. It can handle Unicode characters as well. The method returns True
if all the characters in the string are numeric, otherwise False
. Understanding the use of isnumeric()
method in Python can be helpful in handling string operations.