| by Arround The Web | No comments

Python Check if Character is Number

There often comes a time when the developer needs to verify whether the character inserted by the user is a number. In Python, the user can do this in multiple ways, which include the use of simple “if-else statements”, ASCII equivalents in if-else, the isdigit() method, and the isnumeric() method.

This post will serve as a user guide to check a character against numbers.

Method 1: Using if-else Statements

Starting off with the most basic approach, the user can simply use the if-else conditional statements to check against character “0” to character “9”. To better explain this method, let’s take the following example below:

charVar = '2'
if (charVar >= '0' and charVar <='9'):
    print("It is a number")
   
else:
    print("It is not a number")

 
In this above code snippet:

    • A character variable is initialized with the character ‘2’.
    • If the statement is used to check if the character is anywhere between 0 to 9.
    • If the result is true, then it prints that it is indeed a number.
    • Otherwise, it is not a number.

When the above code is executed, it produces the following result:


From the output, it can be clearly seen that the result is indeed correct. However, let’s change the value of the charVar to something that is not a number like:

charVar = 'M'

 
This time, when the code is executed, it produces the following result:


The output verifies that the character ‘M’ is not a number.

Method 2: Using ASCII Codes in If-else

An alternative to string/character comparison in the if-else conditional statements is to use the ASCII code comparison in if-else statements. However, to get the ASCII code of a character in Python, the “ord()” method is used. And then for comparison, you need to compare it against the values from “48” and “57”. To showcase the working of this method, take the following code:

charVar = '7'
if (ord(charVar) >= 48 and ord(charVar) <=57):
    print("It is a number")
   
else:
    print("It is not a number")

 
In the above code:

    • A character variable “charVar” is initialized with the value ‘7’.
    • Inside the if-statements the ord() method is used to get the ASCII code of the value of charVar.
    • The ASCII is compared against values from 48 to 57 which represent the number 0 to 9.
    • Result is printed according to the result of the if statement.

When the above code is executed, it will produce the following result on the terminal:


After these if-else statements, the user can also use the built-in method, which will be covered next.

Method 3: Using the isdigit() Method

The isdigit() method can be applied to a string or a character variable with the help of a dot operator and it will return “true” or “false”, depending upon whether the character is a number or not. For this method, simply take the below-given code:

charVar = '7'
print(charVar.isdigit())

 
When this code is executed, it produces the following outcome on the terminal:


This means that the charVar is indeed a number. However, make a note of the limitation of the isdigit() method that it cannot be used to deduct negative integers.

Method 4: Using the isnumeric() Method

With very similar working to isdigit(), the isnumeric() method can be used to check whether a character inside a variable is a number or not. Unlike the isdigit() method, the isnumeric() method can also be used to detect negative integer values. To test out the working of the isnumeric() method, simply use the following code:

charVar = '4'
print(charVar.isnumeric())

 
The code will produce the following outcome:


The result verifies that a character is indeed a number.

Bonus Method: Using Type Casting and Error Handling

When an invalid type conversion is made, the program runs into an error and crashes. The crash can be avoided by using error handling inside the “try-catch” blocks. To use this approach, use the following code:

new_str = '1'
try:
    int(new_str)
    print("It is a number")
except ValueError:
    print("It is not a number")

 
In this short code snippet:

    • The string variable is converted into an integer using the “int()” method.
    • If the conversion is successful, the program prompts the user that the character was an integer.
    • Otherwise, it prints that the character is not a number.

When this code is run, it will produce the following result on the terminal:


That was all about checking if a character is a number or not in Python.

Conclusion

The user can easily check whether or not a character is a number or not by using if-else conditional statements, built-in methods, and type casting with an error handling technique. For the if-else conditional statements, simply use the condition to check for the characters 0 to 9 or their ASCII code. For the built-in methods, simply apply them to the character variable by using a dot operator.

Share Button

Source: linuxhint.com

Leave a Reply