
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if Raw Input is Integer in Python 3
In Python 3, the input() function always returns a string, even if the user enters a number. To check if the user input is an integer, you can use a try-except block and attempt to convert the input string to an integer using the int() function. Here are some code examples that demonstrate different ways to check if raw input is an integer in Python 3:
Using a try-except block
Example
In this example, we use a try-except block to attempt to convert the user's input to an integer using the int() function. If the user enters an integer, the conversion succeeds and the program prints "User input is an integer". If the user enters a non-integer value, the int() function will raise a ValueError exception, which we catch with the except block and print "User input is not an integer".
try: user_input = int(input("Enter an integer: ")) print("User input is an integer") except ValueError: print("User input is not an integer")
Output
Enter an integer: 45 User input is an integer
Using the isdigit() method
Example
In this example, we use the isdigit() method to check if every character in the user's input is a digit. If every character is a digit, then the input is an integer and the program prints "User input is an integer". If there is at least one non-digit character in the input, then the input is not an integer and the program prints "User input is not an integer".
user_input = input("Enter an integer: ") if user_input.isdigit(): print("User input is an integer") else: print("User input is not an integer")
Output
Enter an integer: lorem User input is not an integer
Using a regular expression
Example
In this example, we use the re.match() function to match the user's input to a regular expression that only matches strings consisting of one or more digits. If the user's input matches the regular expression, then the input is an integer and the program prints "User input is an integer". If the input does not match the regular expression, then the input is not an integer and the program prints "User input is not an integer".
import re user_input = input("Enter an integer: ") if re.match(r"^[0-9]+$", user_input): print("User input is an integer") else: print("User input is not an integer")
Output
Enter an integer: 99 User input is an integer
Example
In this example, we first prompt the user for input using the input() function. Then, we use a try-except block to attempt to convert the input to an integer using the int() function. If the conversion is successful, we print a message indicating that the input is an integer. If the conversion raises a ValueError, we catch the error and print a message indicating that the input is not an integer.
# Prompt user for input user_input = input("Enter an integer: ") # Use try-except block to check if input is an integer try: integer = int(user_input) print("The input is an integer.") except ValueError: print("The input is not an integer.")
Output
Enter an integer: Ipsum The input is not an integer.
Example
In this example, we first prompt the user for input using the input() function. Then, we use the isdigit() method to check if the input is an integer. If the method returns True, we print a message indicating that the input is an integer. If the method returns False, we print a message indicating that the input is not an integer.
# Prompt user for input user_input = input("Enter an integer: ") # Check if input is an integer using isdigit() method if user_input.isdigit(): print("The input is an integer.") else: print("The input is not an integer.")
Output
Enter an integer: 2023 The input is an integer.
Example
In this example, we first prompt the user for input using the input() function. Then, we use a regular expression to check if the input is an integer. The regular expression ^[0-9]+$ matches any string that contains only digits. If the input matches the regular expression, we print a message indicating that the input is an integer. If the input does not match the regular expression, we print a message indicating that the input is not an integer.
# Prompt user for input user_input = input("Enter an integer: ") # Use regular expression to check if input is an integer import re if re.match("^[0-9]+$", user_input): print("The input is an integer.") else: print("The input is not an integer.")
Output
Enter an integer: 1221 The input is an integer.