Open In App

PyInputPlus module in Python

Improve
Improve
Like Article
Like
Save
Share
Report

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 :

  • inputNum() : Accepts a numeric value. It takes additional parameters ‘min’, ‘max’, ‘greaterThan’ and ‘lessThan’ for bounds. Returns an int or float.
  • inputStr() : Accepts a string value. It provides features such as ‘blockRegexes’, ‘timeout’, ‘limit’ etc.
  • inputInt() : Accepts an integer value. This also takes additional parameters ‘min’, ‘max’, ‘greaterThan’ and ‘lessThan’  for bounds. Returns an int.
  • inputFloat() : Accepts a floating-point numeric value. Also takes additional ‘min’, ‘max’, ‘greaterThan’ and ‘lessThan’  parameters. Returns a float.
  • inputBool() : Accepts a boolean value. Input can be any case-insensitive form of ‘true’/’false’ and ‘T’/’F’. Returns a boolean value.
  • inputChoice() : Takes a list of strings and accepts one of them as input.
  • inputMenu() : Similar to inputChoice(), but the choices are presented as items in a menu. Menu items can be marked with letters or numbers, using the ‘lettered’ and ‘numbered’ parameters.
  • inputDate()  : Accepts a date in a strftime format. We need to pass this format to the ‘formats’ parameter. Returns a datetime.date object.
  • inputTime() : Accepts a time in a strftime format. Returns a datetime.time object.
  • inputDatetime() :  Accepts a date and time in a strftime format. Returns a datetime.datetime object.
  • inputYesNo() : Accepts ‘yes’/’no’ or ‘y’/’n’ in case-insensitive form. Returns ‘yes’ or ‘no’.

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.

Python3




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.

Python3




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.

Python3




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.

Python3




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 ‘*‘.

Python3




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. 

Python3




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


Last Updated : 22 Jun, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads