Mastering Core PyInputPlus Functions:
-inputDate()
-inputYesNo()
-inputBool()
-inputEmail()
-inputFilepath()
-inputPassword()
PyInputPlus enhances Python's basic input() function by offering validated input prompts. This guide focuses on the most practical functions used in daily scripts and form validation.
inputDate(): Input and Validate Date
import pyinputplus as pyip
from datetime import datetime
date = pyip.inputDate("Enter your birth date (YYYY-MM-DD): ", formats=["%Y-%m-%d"])
print("Parsed date:", date)
print("Day:", date.day, "Month:", date.month, "Year:", date.year)
Explanation:
- Prompts the user for a date input and converts it into a
datetime.dateobject. - The
formatsargument accepts a list of valid input formats; here it expects exactlyYYYY-MM-DD. - If the input doesn't match the format or is invalid (e.g., month > 12), PyInputPlus re-prompts until valid or until limit/timeout if specified.
Sample Output (terminal):
Enter your birth date (YYYY-MM-DD): 2000-12-01
Parsed date: 2000-12-01
Day: 1 Month: 12 Year: 2000
inputYesNo(): Get a Yes/No Response
import pyinputplus as pyip
response = pyip.inputYesNo("Do you want to continue? ")
print("Response:", response)
Explanation:
- Accepts inputs:
yes,no,y, orn, case-insensitively. - Returns exactly
"yes"or"no"(normalized to lowercase). - If the user enters anything else, it shows a default error message and prompts again (unless limit/timeout/default used).
Sample Output (terminal):
Do you want to continue? y
Response: yes
inputBool(): Convert Yes/No to Boolean
import pyinputplus as pyip
confirm = pyip.inputBool("Do you accept the terms? ")
print("Accepted:", confirm)
Explanation:
- Uses the same valid entries as
inputYesNo()(yes/no/y/n). - Returns
Trueif the user types yes/y,Falseif no/n. - Ideal for binary decisions in code, so you can write:
if pyip.inputBool(...): ...
Sample Output (terminal):
Do you accept the terms? no
Accepted: False
inputEmail(): Validate Email Format
import pyinputplus as pyip
email = pyip.inputEmail("Enter your email address: ")
print("Email:", email)
Explanation:
- Ensures the input is a syntactically correct email address (checks for pattern like
user@domain.tld). - Rejects inputs missing
@, missing domain, or invalid characters. - On invalid input, displays an error and re-prompts until valid (or until limit/timeout/default).
Sample Output (terminal):
Enter your email address: user@domain.com
Email: user@domain.com
inputFilepath(): Accept Valid File Paths
import pyinputplus as pyip
# exists=False means it only checks syntax, not actual existence
path = pyip.inputFilepath("Enter file path: ", exists=False)
print("Path entered:", path)
Explanation:
- Validates that the input follows valid file path syntax for the OS (e.g., correct use of separators).
- If
exists=Trueis passed, it also checks that the file actually exists and re-prompts if not. - Useful when you need to ensure user-specified paths meet requirements before opening files.
Sample Output (terminal):
Enter file path: C:\Users\Atul\Documents\log.txt
Path entered: C:\Users\Atul\Documents\log.txt
inputPassword(): Secure Hidden Input
import pyinputplus as pyip
pwd = pyip.inputPassword("Enter your password: ")
print("Password length:", len(pwd))
Explanation:
- Does not echo typed characters on the screen for security.
- Returns the entered password as a string, so you can compare or hash it afterward.
- If you want minimum length or other rules, combine with
limitor custom validation in code.
Sample Output (terminal):
Enter your password:
Password length: 8
📌 Common Date Format Specifiers for inputDate():
%Y: Four-digit year (e.g., 2024)%m: Two-digit month (01–12)%d: Two-digit day of month (01–31)%b: Abbreviated month name (e.g., Jun)%B: Full month name (e.g., June)
These core PyInputPlus functions make handling validated user input simple, clean, and secure—perfect for command-line tools, automation scripts, and interactive Python applications.
📚 Related Topics from PyInputPlus:
- ➤ Introduction to PyInputPlus
- ➤ inputStr() from PyInputPlus
- ➤ inputNum() from PyInputPlus
- ➤ inputChoice() from PyInputPlus
- ➤ inputMenu() from PyInputPlus
- ➤ Other Important PyInputPlus Functions