Guide: How to check if an input is not a number in Python

Welcome to our comprehensive guide on how to determine if an input is not a number in Python. In this tutorial, we will cover various methods and techniques to identify whether a user-provided input is a numeric value or not. We’ll explore both formal and informal ways to accomplish this task, providing you with examples and tips along the way.

Using Python Built-in Functions

Python offers several built-in functions that can help us determine if a given input is a number or not. Let’s take a look at a few of these functions:

The isdigit() Function

The isdigit() function is a helpful method in Python that returns True if all the characters in a string are digits, and False otherwise. Let’s see an example:

Example 1:

 input_str = input("Enter a value: ") if input_str.isdigit(): print("The input is a positive integer.") else: print("The input is not a positive integer.") 

If the user enters a positive integer, the output will be “The input is a positive integer.” Otherwise, it will display “The input is not a positive integer.”

The isnumeric() Function

Similar to isdigit(), the isnumeric() function checks if all characters in a string are numeric. However, it also considers other numeric characters such as superscripts, subscripts, and fractions. Here’s an example:

Example 2:

 input_str = input("Enter a value: ") if input_str.isnumeric(): print("The input is numeric.") else: print("The input is not numeric.") 

This code will output “The input is numeric” if the user enters any numeric value. Otherwise, it will display “The input is not numeric.”

The try-except Block

Another approach to check if a value is not a number in Python is by using a try-except block. Consider the following example:

Example 3:

 try: input_num = float(input("Enter a number: ")) print("The input is a number.") except ValueError: print("The input is not a number.") 

In this code snippet, we attempt to convert the user’s input to a float using float(). If the input can be successfully converted, it means it’s a number. Otherwise, a ValueError will be raised, and we’ll print “The input is not a number.”

Additional Tips and Tricks

Handling Decimal Numbers

If you want to consider decimal numbers as valid, you can replace float() with Decimal() in the try-except block. Make sure to import the Decimal module from the decimal library before using it.

Handling Negative Integers

If you also want to handle negative integers, you can modify the isdigit() example as follows:

Example 4:

 input_str = input("Enter a value: ") if input_str[1:].isdigit() and input_str[0] == "-": print("The input is a negative integer.") else: print("The input is not a negative integer.") 

This code will output “The input is a negative integer” for negative integers and “The input is not a negative integer” for other cases.

Handling Floating-Point Numbers

If you specifically want to handle floating-point numbers, you can modify the isnumeric() example as follows:

Example 5:

 input_str = input("Enter a value: ") try: float(input_str) print("The input is a floating-point number.") except ValueError: print("The input is not a floating-point number.") 

If the input can be successfully converted to a float using float(), it means it’s a floating-point number. Otherwise, “The input is not a floating-point number” will be printed.

Conclusion

Congratulations! You’ve now learned various ways to check if an input value is not a number in Python. We covered methods such as isdigit() and isnumeric(), as well as utilizing try-except blocks to handle exceptions. We also provided additional tips to handle decimal numbers, negative integers, and floating-point numbers. Now you have the tools to validate numeric inputs effectively. Happy coding!

0 0 votes
Article Rating
⭐Share⭐ to appreciate human effort 🙏
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
Scroll to Top