Input Validation in Python
Last Updated :
18 Apr, 2025
Input validation ensures that data entered by the user is correct, safe, and in the expected format. In Python, input validation is essential for creating robust, error free programs that can handle incorrect or unexpected inputs. Python provides several ways to validate user inputs, let's explore some.
Using try-except for Type Validation
One of the simplest methods to ensure the input is of the correct type is to use a try-except block. For example, when accepting numeric input, we can ensure that the user enters a valid integer.
Python
while True:
try:
num = int(input("Enter a number: "))
break
except ValueError:
print("Invalid input!")
print(num)
Output
Enter a number: 5
5
Explanation: If the user enters something that cannot be converted to an integer (like a string), a ValueError is raised, and the user is prompted again.
Using if Statements for Range Validation
For situations where you need to ensure the input is within a certain range, you can use a simple if statement.
Python
while True:
age = int(input("Enter age: "))
if 0 <= age <= 120:
break
else:
print("Enter a valid age inside the range (0-120)")
print(age)
Output
Enter age: 144
Enter a valid age inside the range (0-120)
Enter age: 23
23
Explanation: In this case, the function will repeatedly prompt the user until they enter a valid age.
For more complex input validation, such as ensuring an email address or phone number follows a particular format, regular expressions (regex) are useful.
Python
import re
while True:
email = input("Enter email: ")
pattern = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$"
if re.match(pattern, email):
break
else:
print("Invalid email format")
print(email)
Output
Enter email: aryan.tanwar
Invalid email format
Enter email: [email protected]
[email protected]
Explanation: regular expression pattern ensures that the input matches the typical structure of an email address.
Related Articles:
Similar Reads
Input Validation in Python String In Python, string input validation helps ensure that the data provided by the user or an external source is clean, secure and matches the required format. In this article, we'll explore how to perform input validation in Python and best practices for ensuring that strings are correctly validated.Pyt
2 min read
Python String Input Output In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output.Input operations in PythonPythonâs input() function allows us to get data from the u
3 min read
Get User Input in Loop using Python In Python, for and while loops are used to iterate over a sequence of elements or to execute a block of code repeatedly. When it comes to user input, these loops can be used to prompt the user for input and process the input based on certain conditions. In this article, we will explore how to use fo
3 min read
List As Input in Python in Single Line Python provides several ways to take a list as input in Python in a single line. Taking user input is a common task in Python programming, and when it comes to handling lists, there are several efficient ways to accomplish this in just a single line of code. In this article, we will explore four com
3 min read
How to Take Array Input in Python Using NumPy NumPy is a powerful library in Python used for numerical computing. It provides an efficient way to work with arrays making operations on large datasets faster and easier. To take input for arrays in NumPy, you can use numpy.array. Taking Array Input Using numpy.array()The most simple way to create
3 min read