Python String isnumeric() Method with Examples

In the previous article, we have discussed Python Program for help() Function with Examples
isnumeric() Method in Python:

If all of the characters are numeric (0-9), the isnumeric() method returns True; otherwise, it returns False.

Exponents such as ² and ¾ are also considered numeric values.

Because all of the characters in the string must be numeric, “-2” and “1.4” are NOT considered numeric values.

And the ‘-‘, ‘.’ are not the numeric values.

Numeric characters in Python include decimal characters (such as 0, 1, 2..), digits (such as subscript, superscript), and characters with the Unicode numeric value property (such as fractions, roman numerals, currency numerators).

Syntax:

string.isnumeric()

Parameters: This function has no parameters.

Return Value:

The isnumeric() method gives:

  • True if all of the characters in the string are numbers.
  • If at least one of the characters is not a numeric character, the statement is false.

Examples:

Example1:

Input:

Given string = '-6'
Given string = '5/6'
Given string = 'btechgeeks 12345'

Output:

The given string =  -6
Checking if the given string has all numeric characters or not =  False
The given string =  5/6
Checking if the given string has all numeric characters or not =  False
The given string =  btechgeeks 12345
Checking if the given string has all numeric characters or not =  False

Example2:

Input:

Given string = '\u0030'
Given string = '1.7'
Given string = '\u00B2'

Output:

The given string = 0
Checking if the given string has all numeric characters or not = True
The given string = 1.7
Checking if the given string has all numeric characters or not = False
The given string = ²
Checking if the given string has all numeric characters or not = True

String isnumeric() Method Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the string as static input and store it in a variable.
  • Apply isnumeric() function to the given string in which If all of the characters are numeric (0-9), the isnumeric() method returns True; otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all numeric characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
# unicode for the number 0
gvn_strng1 = '\u0030'
# Apply isnumeric() function to the given string in which If all of the
# characters are numeric (0-9), the isnumeric() method returns True;
# otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isnumeric()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all numeric
# characters or not.
print("Checking if the given string has all numeric characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 = '1.7'
rslt_2 = gvn_strng2.isnumeric()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all numeric characters or not = ", rslt_2)
# unicode for the value ²
gvn_strng3 = '\u00B2'
rslt_3 = gvn_strng3.isnumeric()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all numeric characters or not = ", rslt_3)

Output:

The given string = 0
Checking if the given string has all numeric characters or not = True
The given string = 1.7
Checking if the given string has all numeric characters or not = False
The given string = ²
Checking if the given string has all numeric characters or not = True

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply isnumeric() function to the given string in which If all of the characters are numeric (0-9), the isnumeric() method returns True; otherwise, it returns False.
  • Store it in another variable.
  • Print the above-given String.
  • Print the result after checking if the given string has all numeric characters or not.
  • Similarly, do the same for other strings and print the result.
  • The Exit of Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_strng1 =  input("Enter some Random String = ")
# Apply isnumeric() function to the given string in which If all of the
# characters are numeric (0-9), the isnumeric() method returns True;
# otherwise, it returns False.
# Store it in another variable.
rslt_1 = gvn_strng1.isnumeric()
# Print the above-given String.
print("The given string = ", gvn_strng1)
# Print the result after checking if the given string has all numeric
# characters or not.
print("Checking if the given string has all numeric characters or not = ", rslt_1)
# Similarly, do the same for other strings and print the result.
gvn_strng2 =  input("Enter some Random String = ")
rslt_2 = gvn_strng2.isnumeric()
print("The given string = ", gvn_strng2)
print("Checking if the given string has all numeric characters or not = ", rslt_2)
gvn_strng3 = input("Enter some Random String = ")
rslt_3 = gvn_strng3.isnumeric()
print("The given string = ", gvn_strng3)
print("Checking if the given string has all numeric characters or not = ", rslt_3)

Output:

Enter some Random String = -10.5
The given string = -10.5
Checking if the given string has all numeric characters or not = False
Enter some Random String = 5/6
The given string = 5/6
Checking if the given string has all numeric characters or not = False
Enter some Random String = btechgeeks 12345
The given string = btechgeeks 12345
Checking if the given string has all numeric characters or not = False

Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.