Beyond input(): Modern Python Techniques for Bulletproof User Input Validation
Still using input()
in your python scripts ?, the same old input method without any data validation and writing it all by yourself. No need to do that any more.
After going through this article, you will be able to validate data input like a PRO!. Thats right, in this article we will see how to validate data input, mask user input in stdio screen, accept data from choices and loop until a valid response is received using python library Rich.
Table of Contents
Installation
Rich can be installed using pip
from PyPI or your favorite package manager like rye, uv or poetry etc.
For using rich in Jupyter Notebook or Jupyter Lab, install with additional dependencies using rich[jupyter]
Rich has a number of Prompt
classes to ask user for input and loop until it is a valid response. It also provides options to specify default value, choose from a options, entering passwords etc.
prompt | Response type |
---|---|
Prompt |
String |
IntPrompt |
Integer |
FloatPrompt |
Float |
Confirm |
Yes / No |
Prompt
Prompt.ask()
method returns strings,
- Set a default value, which will be returned if the user press enter or return key without entering any value.
- Supply a list of choices, the prompt will loop until the user enters one of the choices.
from rich.prompt import Prompt
from rich import print
name = Prompt.ask("Enter your name")
print(f"{type(name)=}")
print(f"{name=}")
Default value
use default
argument to return default value on pressing return key without entering any text.
from rich.prompt import Prompt
from rich import print
name = Prompt.ask("Enter your name", default="Aavishkarah")
print(f"{type(name)=}")
print(f"{name=}")
Password Input
By default password
argument is set to False
in prompt.ask()
, by setting it to True
, the entered text can be hidden from the terminal.
from rich.prompt import Prompt
from rich import print
password = Prompt.ask("Enter your password", password=True)
print(f"{password=}")
Choices
By supplying the list of choices, the prompt will loop until the user enters one of the choices.
from rich.prompt import Prompt
from rich import print
programming_languages = [
"Python",
"Rust",
"C",
"Cpp",
"Java",
"Go",
]
language = Prompt.ask("Enter your programming language", choices=programming_languages)
print(f"{language=}")
Case Insensitive Choices
By default case_sensitive
is True
, by setting it to False
choices can be case insensitive.
Hide Choices
By default show_choices
is True
, by setting it to False
you can hide choices list on the std output screen.
IntPrompt
By default Prompt.ask()
return string. IntPrompt.ask()
returns integer data and its keeps looping until an integer data is entered. So no need of converting input data in to integer and writing messy try
and except
blocks.
from rich.prompt import IntPrompt
from rich import print
number = IntPrompt.ask("Enter a number")
print(f"{type(number)=}")
print(f"{number=}")
FloatPrompt
Similar to IntPrompt.ask()
returns integer data, FloatPrompt.ask()
keeps looping until a float is entered.
from rich.prompt import FloatPrompt
from rich import print
number = FloatPrompt.ask("Enter a float value")
print(f"{type(number)=}")
print(f"{number=}")
Confirm
Confirm.ask()
method is used to ask a yes
or no
question format.
from rich.prompt import Confirm
from rich import print
answer = Confirm.ask("Choose your option")
print(f"{type(answer)=}")
print(f"{answer=}")