Python String isnumeric() Method (original) (raw)
Last Updated : 11 Jul, 2023
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
string
=
'123geeks456for789geeks'
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: geeksforgeeks
Errors and Exceptions
It does not contain any arguments, therefore, it returns an error if a parameter is passed.
Python3
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
s
=
" "
p
=
"12 3"
print
(s.isnumeric())
print
(p.isnumeric())
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'
print
(string1.isnumeric())
print
(string2.isnumeric())
print
(string3.isnumeric())
print
(string4.isnumeric())
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
number
=
75
string
=
str
(number)
result
=
string.isnumeric()
print
(result)
number
=
5.65
string
=
str
(number)
result
=
string.replace(
'.'
, '',
1
).isnumeric()
print
(result)
Output:
True True