Updated April 14, 2023
Introduction to Python Validation
Whenever the user accepts an input, it needs to be checked for validation which checks if the input data is what we are expecting. The validation can be done in two different ways, that is by using a flag variable or by using try or except which the flag variable will be set to false initially and if we can find out that the input data is what we are expecting the flag status can be set to true and find out what can be done next based on the status of the flag whereas while using try or except, a section of code is tried to run. If there is a negative response, then the except block of code is run.
Types of Validation in Python
There are three types of validation in python, they are:
- Type Check: This validation technique in python is used to check the given input data type. For example, int, float, etc.
- Length Check: This validation technique in python is used to check the given input string’s length.
- Range Check: This validation technique in python is used to check if a given number falls in between the two numbers.
The syntax for validation in Python is given below:
Syntax using the flag:
flagName = False
while not flagName:
if [Do check here]:
flagName = True
else:
print('error message')
The status of the flag is set to false initially, and the same condition is considered for a while loop to make the statement while not true, and the validation is performed setting the flag to true if the validation condition is satisfied; otherwise, the error message is printed.
Syntax using an exception:
while True:
try:
[run code that might fail here]
break
except:
print('This is the error message if the code fails')
print('run the code from here if code is successfully run in the try block of code above')
print(‘run the code from here if code is successfully run in the try block of code above)
We set the condition to be true initially and perform the necessary validation by running a block of code, and if the code fails to perform the validation, an exception is raised displaying the error message and a success message is printed if the try block successfully executes the code.
Examples of Python Validation
Examples of python validation are:
Example #1
Python program using a flag to validate if the input given by the user is an integer.#Datatype check.
#Declare a variable validInt which is also considered as flag and set it to false
validInt = False
#Consider the while condition to be true and prompt the user to enter the input
while not validInt:
#The user is prompted to enter the input
age1 = input('Please enter your age ')
#The input entered by the user is checked to see if it’s a digit or a number
if age1.isdigit():
#The flag is set to true if the if condition is true
validInt = True
else:
print('The input is not a valid number')
#This statement is printed if the input entered by the user is a number
print('The entered input is a number and that is ' + str(age1))
Output:
Example #2
Python program uses flag and exception to validate the type of input given by the user and determine if it lies within a given range. #Range Check.
Code:
#Declare a variable areTeenager which is also considered as flag and set it to false
areTeenager = False
#Consider the while condition to be true and prompt the user to enter the input
while not areTeenager:
try:
#The user is prompted to enter the input
age1 = int(input('Please enter your age '))
#The input entered by the user is checked if it lies between the range specified
if age1 >= 13 and age1 <= 19: areTeenager = True
except:
print('The age entered by you is not a valid number between 13 and 19')
#This statement is printed if the input entered by the user lies between the range of the number specified
print('You are a teenager whose age lies between 13 and 19 and the entered age is ' + str(age1))
Output:
Example #3
Python program using the flag to check the length of the input string. #Length Check.
Code:
#Declare a variable lenstring which is also considered as flag and set it to false
lenstring = False
#Consider the while condition to be true and prompt the user to enter the input
while not lenstring:
password1 = input('Please enter a password consisting of five characters ')
#The input entered by the user is checked for its length and if it is below five
if len(password1) >= 5:
lenstring = True
else:
print('The number of characters in the entered password is less than five characters')
#This statement is printed if the input entered by the user consists of less than five characters
print('The entered password is: ' + password1)
Output:
Benefits
- Validation in python helps to improve the security of code.
- It prevents third-party users from mishandling the code accidentally or intentionally.
- It can be used to check if the input data type is correct or not.
- It can be used to check if there are no invalid values in the given input.
- It can be used to check if the given input lies in the range or is it out of range.
- It can be used to check if the given input meets the constraints specified on them.
- It can be used to check if the given input is consistent or not.
- It can be used to check if the given input is valid.
- It can be used to check if the given input is complete or incomplete.
Recommended Articles
We hope that this EDUCBA information on “Python Validation” was beneficial to you. You can view EDUCBA’s recommended articles for more information.