.isnumeric()
.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 variablesnum = "2"one_half = "½"year = "2023"# Show the resultsprint(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:
Looking to contribute?
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.