VOOZH about

URL: https://www.geeksforgeeks.org/python/python-string-isnumeric-method/

⇱ Python String isnumeric() Method - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Python String isnumeric() Method

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.

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

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.

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.


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

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


Output
False
False

Subscript, Superscript, Fractions, and Roman numerals (all written in Unicode)are all considered to be numeric, Therefore, it returns "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.

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

Output: 

True
True
Comment
Article Tags: