Open In App

PyInputPlus module in Python

PyInputPlus is a Python module used for taking inputs with additional validation features. PyInputPlus will keep asking the user for text until they enter valid input.

Installation:



Install the module via pip command :

pip install PyInputPlus

Here are the functions used to take various types of input :



Some example usages :

1. String input :

The inputStr() function is used to take string inputs. We can set the prompt parameter to define a prompt before taking the input. It is similar to the prompt parameter of the built-in function input(). We can allow blank values as valid input by setting the blank parameter as True.  Certain inputs can be blocked (that is, these values are invalidated) by setting the blockRegexes parameter’s value as the regex pattern we want to invalidate.




import pyinputplus as pyip
  
# string input with
# additional parameters
inp = pyip.inputStr(prompt="Enter a string... "
                    blank=True, blockRegexes = 'aeiou')
  
print(inp)

Output :

Enter a string... Hello
This response is invalid.
Enter a string... GFG
GFG

2. Integer input :

The inputInt() function is used to take integer inputs. If any non-integer input is passed, the function retries until a valid input is given, time runs out or maximum number of tries exceed. The default parameter is used to set the default value if time runs out or the number of tries exceeds. The limit parameter is used to specify the maximum number of tries the user has for entering a valid input, after which the default value is returned, if specified, or a RetryLimitException is raised otherwise.




import pyinputplus as pyip
  
# integer input with
# limited number of tries
inp = pyip.inputInt(prompt = "Enter an Integer... "
                    default = 0, limit = 3)
  
print(inp)

Output :

Enter an Integer... hello
'hello' is not an integer.
Enter an Integer... abc
'abc' is not an integer.
Enter an Integer... 
Blank values are not allowed.
0

The timeout parameter is used to set the number of seconds since the first prompt after which the default value is returned, if specified, or a TimeoutException is raised otherwise.




import pyinputplus as pyip
  
# integer input with
# limited time
inp = pyip.inputInt(prompt = "Enter an Integer... "
                    default = 0, timeout = 2)
  
print(inp)

Output :

Enter an Integer... 4
0

The min and max parameters can be used to set lower and upper bounds for the input. Similarly, the greaterThan and lessThan parameters are used to set the external bounds.




import pyinputplus as pyip
  
# integer input with
# specific bounds
inp = pyip.inputInt(prompt = "Enter an Integer... ",
                    min = 4, lessThan = 9 )
  
print(inp)

Output :

Enter an Integer... 3
Number must be at minimum 4.
Enter an Integer... 12
Number must be less than 9.
Enter an Integer... 8
8

3. Menu Input :

The inputMenu() function is used to display a menu of valid choices for inputs. The lettered or numbered parameters can be set to True to mark the items as A,B,C… or 1,2,3… respectively. The default is marking is ‘*‘.




import pyinputplus as pyip
  
# menu item input
inp = pyip.inputMenu(['apple', 'banana', 'mango'])
  
print(inp)

Output :

Please select one of the following:
* apple
* banana
* mango
peach
'peach' is not a valid choice.
Please select one of the following:
* apple
* banana
* mango
mANgo
mango

4. Datetime Input : 

The inputDatetime() function accepts a date and time in one of the strftime formats specified in a list or tuple passed to the formats parameter. 




import pyinputplus as pyip
  
# date and time input
inp = pyip.inputDatetime('Enter date and time... '
                         formats = ('%m/%d/%y %H:%M:%S',
                                    '%m.%d.%Y %H:%M:%S') )
  
print(inp)

Output :

Enter date and time... 3.4.18 4:32:16
'3.4.18 4:32:16' is not a valid date and time.
Enter date and time... 3/4/18 4:32:16
2018-03-04 04:32:16

Article Tags :