Friday, November 15, 2024
Google search engine
HomeLanguagesPython String isnumeric() Method

Python String isnumeric() Method

The isnumeric() method is a built-in method in Python that belongs to the string class. It is used to determine whether the string consists of numeric characters or not. It returns a Boolean value. If all characters in the string are numeric and it is not empty, it returns “True” If all characters in the string are numeric characters, otherwise returns “False”.

Example: In this given string we will check string contains numeric characters or not.

Python3




string = "123456789"
result = string.isnumeric()
print(result)


Output:

True

Python String isnumeric() Method Syntax

Syntax:  string.isnumeric()

Parameters: isnumeric() does not take any parameters

Returns :

  • True – If all characters in the string are numeric characters.
  • False – If the string contains 1 or more non-numeric characters.

Ways to Implement the isnumeric() Method in Python

In Python, there are different libraries, functions, and methods to check if strings contain numeric characters. Here are the different ways in which we can use Isnumeric method.

Checking numeric/non-numeric characters using isnumeric() Method in Python

Python3




string = '123ayu456'
print(string.isnumeric())
 
string = '123456'
print(string.isnumeric())


Output: 

False
True

We can use various methods to check if the string contains numeric characters or not. To check this we can use different approach to solve this.

Counting and Removing numeric characters

In this example, the isnumeric() method is used to check the number of numeric characters and the resulting string after removing numeric characters.

Python3




# Given string
string = '123Lazyroar456for789Lazyroar'
count = 0
new_string = ""
 
for ch in string:
    if ch.isnumeric():
        count += 1
    else:
        new_string += ch
 
print("Number of numeric characters:", count)
print("String after removing numeric characters:", new_string)


Output: 

Number of numeric characters: 9
String after removing numeric characters: neveropen

Errors and Exceptions

It does not contain any arguments, therefore, it returns an error if a parameter is passed.

Python3




# isnumeric() returns an error if a parameter is passed
String = "1234567"
 
try:
    String.isnumeric("abc")
except TypeError:
    print("TypeError: isnumeric() takes no arguments (1 given)")


Output

TypeError: isnumeric() takes no arguments (1 given)

White spaces are not considered to be numeric, therefore, it returns “False”.

Python3




# isnumeric() to check White-spaces
s = " "
p = "12 3"
 
print(s.isnumeric())  # False
print(p.isnumeric())  # False
# This code is contributed by Susobhan Akhuli


Output

False
False

Subscript, Superscript, Fractions, and Roman numerals (all written in Unicode)are all considered to be numeric, Therefore, it returns “True”.

Python3




string1 = '123'
string2 = '⅓'
string3 = '²'
string4 = '2167'  # 'Ⅷ'; ROMAN NUMERAL EIGHT
 
print(string1.isnumeric())  # True
print(string2.isnumeric())  # True
print(string3.isnumeric())  # True
print(string4.isnumeric())  # True


Output

True
True
True
True

Combining isnumeric() with conditions

In this example, the isnumeric() method is used to check if the string “75” consists of only numeric characters.

Python3




string = '75'
if string.isnumeric() and int(string) > 50:
    print("Valid Number")
else:
    print("Invalid Number")


Output: 

Valid Number

String isnumeric() with another numeric type

The isnumeric() method in Python is primarily designed to work with strings. In this example, we can see the isnumeric() method may not directly support other numeric types like integers or floats, but still can utilize in combination with type conversion to perform numeric validation

Python3




# integer validation
number = 75
string = str(number)
result = string.isnumeric()
print(result)
 
# float validation
number = 5.65
string = str(number)
result = string.replace('.', '', 1).isnumeric()
print(result)


Output: 

True
True

RELATED ARTICLES

Most Popular

Recent Comments