String isnumeric() Method in Python

Python String isnumeric() Method

The isnumeric() method in Python is used to check whether a string contains only numeric characters. It returns True if all the characters in the string are numeric (0-9) characters, and there is at least one character in the string. Otherwise, it returns False.Here's the syntax of the isnumeric() method:

string.isnumeric()

Example:

# Examples of isnumeric() method
string1 = "12345"
string2 = "123.45"
string3 = "١٢٣٤٥"  # Arabic numerals
string4 = "१२३४५"  # Devanagari numerals
string5 = "Hello"

result1 = string1.isnumeric()
result2 = string2.isnumeric()
result3 = string3.isnumeric()
result4 = string4.isnumeric()
result5 = string5.isnumeric()

print("String 1:", string1)
print("String 2:", string2)
print("String 3:", string3)
print("String 4:", string4)
print("String 5:", string5)

print("isnumeric() result for String 1:", result1)
print("isnumeric() result for String 2:", result2)
print("isnumeric() result for String 3:", result3)
print("isnumeric() result for String 4:", result4)
print("isnumeric() result for String 5:", result5)

Output:

String 1: 12345
String 2: 123.45
String 3: ١٢٣٤٥
String 4: १२३४५
String 5: Hello
isnumeric() result for String 1: True
isnumeric() result for String 2: False
isnumeric() result for String 3: True
isnumeric() result for String 4: True
isnumeric() result for String 5: False

In this example, string1 contains only numeric characters ("12345"), so isnumeric() returns True. string2 contains a decimal point, so it is not composed entirely of numeric characters, and thus isnumeric() returns False. string3 and string4 contain non-Latin numerals (Arabic and Devanagari numerals, respectively), and they are considered numeric characters, so isnumeric() returns True for both. string5 contains letters, so it is not composed of numeric characters, and isnumeric() returns False.

The isnumeric() method is useful when you want to check if a string contains only numeric characters, for example, when processing user input that should be in a numerical format or performing validation for numeric input.