Python String isnumeric() Method

Python String Isnumeric() Method

This is a tutorial of the Python String isnumeric() method. Learn to check if a given string only contains numeric characters or not with examples.

Python String isnumeric()

The str.isnumeric() method returns True if the given string contains only numeric characters (0-9, even in subscripts or superscripts, also fractions) and if not it returns False.

Syntax

Given below is the syntax of the str.isnumeric() method.

isnumeric() Parameters

This method does not take any arguments as it only checks the characters in the string on which it is applied, so no additional data is required in the form of arguments.

isnumeric() Return Value

It returns the boolean value True or False according to the following logic.

  • Returns True if all of the characters in the given string are only numerics.
  • Returns False if not all the characters in the given string are numerics.

Examples

Given below are the two examples demonstrating the usage of the Python String isnumeric() method.

Example 1. Checking a given String contains all the numerics or not using str.isnumeric() method

In this example, we’re applying the method isnumeric() on several different strings to check the result, for what strings it returns True and for what it returns False.

#String with only digits
line = "1234567890"
print(line.isnumeric())

#String with digits and a whitespace
line = "12345 67890"
print(line.isnumeric())

#String with digits and special symbols
line = "12345?"
print(line.isnumeric())

#String with Alphanumeric Characters
line = "Hello12345"
print(line.isnumeric())

#String with only Alphabets
line = "Abcd"
print(line.isnumeric())

#String with Powers
line = '12\u00B2'
#represents '12^2'
print(line.isnumeric())

#String with Special Fractions
line = "½"
print(line.isnumeric())

#Floating Point Numbers
line = "5.7"
print(line.isnumeric())

Output.

True
False
False
False
False
True
True
False

Example 2. Using the Python String isnumeric() method with if-else statement

In the following example, we’ve directly used the str.isnumeric() method as the condition for the if-else statement to print a string accordingly after checking if the given string only contains numeric characters or not.

#Using str.isnumeric() with if-else statement

#Given String
name = "12345½12\u00B2"

if name.isnumeric():
    print("name only contains numeric characters.")
else:
    print("name also contains characters other than numerics characters.")

Output.

name only contains numeric characters.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *