Python isnumeric()
method is a built-in string method that represents whether the string contains numeric characters or not. It returns the boolean value True
if all the characters in a string are numeric and the string is not empty, or False
otherwise. Numeric characters include digits from 0 to 9 and any character representing a digit in a non-ASCII script, such as numerals in Arabic, Roman numerals, etc.
In this article, I will explain how to use the Python string isnumeric() method to check if a string contains numeric characters. I will cover the syntax, parameters, and usage of this method and show how it can return a boolean value.
1. Quick Examples of String isnumeric() Method
If you are in a hurry, below are some quick examples of the string isnumeric() method.
# Quick examples of string isnumeric() method
# Example 1: Using string isnumeric() function
string = "12345"
result = string.isnumeric()
# Example 2: Non-numeric characters
# Using string isnumeric() function
string = "012SparkByExamples345"
result = string.isnumeric()
# Example 3: Using string isnumeric()
# With conditions
string = "012345"
if string.isnumeric() and int(string) > 100:
print("Valid Number")
else:
print("Invalid Number")
# Example 4: Using String isnumeric()
# Unicode numeric characters
string = "\u00BD"
result = string.isnumeric()
print("Final result:",result)
# Example 5: Unicode numeric characters
string = "\u2460"
result = string.isnumeric()
# Example 6: Using string isnumeric() function
# With another numeric type
number = 50
string = str(number)
result = string.isnumeric()
# Example 7: Using string isnumeric()
number = 8.49
string = str(number)
result = string.replace('.', '', 1).isnumeric()
# Example 8: Errors and exceptions
string = "12345"
try:
string.isnumeric("abc")
except TypeError:
print("TypeError: Invalid operation on an integer.")
2. Python String isnumeric() Function
The isnumeric()
method in Python is a built-in string method used to check whether a string contains numeric characters. It returns a boolean value. It returns a boolean value: True
if all characters in the string are numeric digits and the string is not empty, or False
otherwise. In other words, if the string contains any non-numeric characters or if it is an empty string, the isnumeric()
method will return False
. This method is useful for checking if a string represents a numerical value.
2.1 Syntax of String isnumeric() Method
Following is the syntax of the string isnumeric() method.
# Syntax of string isnumeric() function
string.isnumeric()
2.2 Parameter of String isnumeric() Method
string
– This is the string on which you want to perform theisnumeric()
method.isnumeric
– This is the method itself. When called on a string, it checks whether all the characters in the string are numeric. It returnsTrue
if all characters are numeric, andFalse
otherwise.
2.3 Return Value
This method returns a boolean value.
- It returns
True
if all characters in the string are numeric(i.e., digits from 0 to 9 and any character that represents a digit in a non-ASCII script). - It returns
False
if the string contains non-numeric characters or is an empty string.
3. Usage of Python String isnumeric() Function
To initialize a string variable string
with the value "12345"
, then check if the string consists of only numeric characters using this function.
In the below example, the isnumeric()
method is applied to the string "12345"
. Since all the characters in the given string are numeric, this function returns True
, which is then printed as the final result.
# Initialize the string
string = "12345"
print("Original string:", string)
# Using string isnumeric() function
result = string.isnumeric()
print("Final result:",result)
Yields below output.
4. Non-numeric Characters Using isnumeric() Method
The string variable string
is initialized with the value "012SparkByExamples345"
. The code then checks if the string consists of only numeric characters using the isnumeric()
function.
However, in this case, the isnumeric()
method returns False
because the string contains non-numeric characters ("SparkByExamples"
) in addition to the numeric characters ("012345"
). Therefore, the final result is False
.
# Initialize the string
string = "012SparkByExamples345"
print("Original string:", string)
# Non-numeric characters
# Using string isnumeric() function
result = string.isnumeric()
print("Final result:",result)
Yields below output.
The isnumeric()
function checks if all the characters in the string are numeric. Since the string contains non-numeric characters, the result is False
.
5. Using String isnumeric() With Conditions
To check if the given string "012345"
contains all numeric characters you can use the isnumeric()
method and conditions. The code then checks if the string is numeric and its integer value is greater than 100, it prints “Valid Number”; otherwise, it prints “Invalid Number”.
The condition string.isnumeric()
evaluates to True
because the string contains only numeric characters. The condition int(string) > 100
also evaluates to True
because the integer value of the string 12345
is greater than 100. Hence, the "Valid Number"
message is printed.
# Initialize the string
string = "012345"
# Using string isnumeric()
# With conditions
if string.isnumeric() and int(string) > 100:
print("Valid Number")
else:
print("Invalid Number")
# Output:
# Valid Number
6. Using String isnumeric() on Unicode Numeric Characters
You can use the isnumeric()
method to check if this Unicode numeric character "\u00BD"
is considered as numeric. This function will return True
since this character is a Unicode numeric character. Therefore, the final result is True
.
# Initialize the string
string = "\u00BD"
print("Original string:", string)
# Using String isnumeric()
# Unicode numeric characters
result = string.isnumeric()
print("Final result:",result)
# Output:
# Original string: ½
# Final result: True
Similarly, you can also use the isnumeric()
method to check if this Unicode numeric character '\u2460'
(which represents the circled digit 1) is considered numeric. This function will return True
since this character is a Unicode numeric character. Therefore, the final result is True
.
# Initialize the string
string = "\u2460"
print("Original string:", string)
# Using String isnumeric()
# Unicode numeric characters
result = string.isnumeric()
print("Final result:",result)
# Output:
# Original string: ①
# Final result: True
7. Using String isnumeric() With Another Numeric Type
In another way, you can check if the string contains all numeric values using isnumeric()
function with another numeric type. First, initialize the integer with the value 50
and then, convert this integer into a string by using str(number)
, and applying this function to a given string, check if the resulting string consists of only numeric characters.
# integer validation
number = 50
# Using string isnumeric() function
# With another numeric type
string = str(number)
result = string.isnumeric()
print(result)
# Output:
# True
You are validating a float number (number = 8.49)
by converting it to a string and then using the isnumeric()
method after removing the decimal point.
In the below example, str(number)
converts the float 8.49
to the string "8.49"
. The replace('.', '', 1)
part removes the first occurrence of the decimal point, resulting in the string "849"
. Then apply this method to this modified string, it returns True
since it contains only numeric characters.
# float validation
number = 8.49
# Using string isnumeric()
string = str(number)
result = string.replace('.', '', 1).isnumeric()
print(result)
# Output:
# True
8. Errors and Exceptions
You can pass an argument to the isnumeric()
method and check whether a string contains all numeric values. This method does not accept any arguments, and trying to pass an argument to this function, will raise a TypeError
.
You can use a try exception code, to except
the error and print the message as TypeError: Invalid operation on an integer.
# Initialize the string
string = "12345"
# Errors and exceptions
try:
string.isnumeric("abc")
except TypeError:
print("TypeError: Invalid operation on an integer.")
# Output:
# TypeError: Invalid operation on an integer.
Conclusion
In this article, I have explained the Python string isnumeric()
method, and using its syntax, parameters, and usage how we can check if all the characters in the string are numeric (0 to 9) and if the string is not empty. Also explained if all characters are numeric and the string is not empty, it returns True
. Otherwise, it returns False
.
Happy Learning !!
Related Articles
- How to Parse a String in Python
- Compare strings using either ‘==’ or ‘is’ sometimes produces a different result.
- Python Extract Numbers From String
- Compare Strings in Python
- Check if String is Empty or Not in Python
- Convert Integer to String in Python
- Trim Whitespace from a String in Python?
- Convert String to Lowercase in Python
- Python Count Specific Character in String
- Python Convert String to Float
- Python Count Characters in String