Javatpoint Logo
Javatpoint Logo

Python Validation

Every time a user submits an input, it must be validated to see if it is exactly what we expected. We can validate the input in one of two ways: by applying a flag variable or implementing try or except. The flag variable will be originally set to false, and if we determine that the given data is the same as our expectation, we will change the flag status to true. From there, we can determine what we can do next, depending on the flag status. If the result is unfavorable, the except code block is executed.

Types of Validation in Python

Broadly we can categorize the validation problems into these three categories:

Type Check: The supplied data type is verified using one of the Python validation methods. The Python data types are such as int, float, etc.

Length Check: The length of the provided input string or any iterable is verified using any of the described Python validation methods.

Range Check: Python's validation method can also be implemented to determine whether the supplied input number falls between the two specified numbers.

Below we have described the Syntax of validation methods:

Syntax using the flag:

The flag's status is originally set to false, and the exact condition is considered during a while loop to keep the statement from becoming true. If the required validation condition is fulfilled, the flag is validated and set to true; if not, an error message is produced.

We first set the condition of the while loop to be true and, after that, run a code block to perform the required validation. If the code cannot complete the verification, an exception will be raised that displays the error message. A success text is printed if the try code block successfully completes the code.

Examples of Python Validation

Examples show the implementation of the validation methods described above.

Example- 1

We will check whether the input has the required data type.

Code

Output

Please type a number: 8.8
The input given is not a valid integer
The given input is an integer and that is: 8.8

Example- 2

The Python program validates the user-provided input using a flag variable to see if it falls inside a certain range.

Code

Output

Please type a number: 7
The number given by you does not lie between 13 and 19
The number given by the user lies between 13 and 19 and the number given by you is: 7

Example- 3

The Python program uses the flag variable to determine the input of the string's length.

Code

Output

Please give a string: Python
The given input string has a length greater than or equal to 5, and the string is: Python

Benefits

  • It aids in enhancing code security.
  • Python's validation feature stops outside users from mistakenly or purposefully misusing the code.
  • We may use it to determine whether or not the given data type is valid.
  • We may use it to determine whether the input provided has any incorrect values.
  • We can use it to determine whether an input falls within or outside the range.
  • We may use it to determine whether the provided input complies with the limits placed on it.
  • We may use it to determine whether or not the input is consistent.
  • We may use Python's validation feature to determine whether an input is legitimate.
  • We may use Python's validation feature to determine whether the provided input variable is complete.






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA