.isnumeric()

Published Sep 22, 2023
Contribute to Docs

.isnumeric() is used to verify that the string variable consists of numerical characters only. Which means the string cannot contain a space, comma, dash, or any other characters that are not numerical.

Syntax

There are two ways of using this function:

str.isnumeric(str_variable)

str_variable.isnumeric()

Example

The following examples implement .isnumeric() and will return True:

print(str.isnumeric("2"))
print(str.isnumeric("½"))
print(str.isnumeric("2023"))

or

# Define the string variables
num = "2"
one_half = "½"
year = "2023"
# Show the results
print(num.isnumeric())
print(one_half.isnumeric())
print(year.isnumeric())

Codebyte Example

This example is runnable and demonstrates instances where non-numeric values are passed into the .isnumeric() function:

Code
Output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy