Skip to main content

askr

Simple, validated input prompts for the terminal.

Every function keeps asking until the answer is valid, so you never have to write another while True / try / except ValueError loop around input() again.

  • No dependencies
  • Fully typed (py.typed, checked with mypy --strict)
  • Defaults, custom validators and attempt limits on every prompt
  • All texts replaceable, German translation included

Installation

pip install askr

Requires Python 3.10 or newer.

Quick start

from askr import ask_choice, ask_int, ask_yn

age = ask_int("Your age: ", min_value=0, max_value=120, default=18)
mode = ask_choice("Difficulty?", ["Easy", "Medium", "Hard"], default="Medium")

if ask_yn("Start the game?"):
    ...

What the user sees:

Your age [18]: abc
Invalid entry. Please enter a whole number.
Your age [18]: 200
The number must be between 0 and 120.
Your age [18]: 25
Difficulty?
  1. Easy
  2. Medium (default)
  3. Hard
Your choice: hard
Start the game? Y/n:

All functions

Function Returns Checks
ask_yn bool y / yes / n / no; Enter returns default
ask_int int whole number, optional min_value / max_value
ask_float float finite number, optional min_value / max_value
ask_str str optional min_length / max_length; strips whitespace by default
ask_email str name@domain.tld, optional allowed_domains
ask_url str https:// / http:// (configurable), adds the scheme if missing
ask_choice str one option, by number or name
ask_multi_choice list[str] several options: 1, 3, 1-3 or names
ask_enum Enum member one member, by number, name or value
ask_list list comma-separated values, converted with int, float, ...
ask_password str hidden input, confirmation, length, character classes, regex
ask_path Path must exist (optional), file or folder only (optional)
ask_date date one or more formats, optional min_date / max_date
ask_time time one or more formats, optional min_time / max_time
ask_datetime datetime one or more formats, optional bounds
ask_confirm_text bool asks once: did the user type the exact word?

Examples

from datetime import date
from enum import Enum
import askr

# Text, email and URL
name = askr.ask_str("Name: ", min_length=1, max_length=40)
email = askr.ask_email("Email: ", allowed_domains=["prevolut.uk"])
site = askr.ask_url("Website: ")                    # "example.com" -> "https://example.com"

# Several values at once
tags = askr.ask_list("Tags: ", unique=True)          # "python, cli" -> ["python", "cli"]
scores = askr.ask_list("Scores: ", int, min_items=3)
days = askr.ask_multi_choice("Days?", ["Mon", "Tue", "Wed", "Thu", "Fri"])  # "1-3, fri"

# Enums
class Size(Enum):
    SMALL = "small"
    LARGE = "large"

size = askr.ask_enum("Size?", Size, default=Size.SMALL)

# Passwords
password = askr.ask_password(confirm=True, min_length=8, require_upper=True,
                             require_digit=True, require_special=True)
pin = askr.ask_password("PIN: ", pattern=r"\d{4}", pattern_hint="be exactly 4 digits")

# Files, dates and times
config = askr.ask_path("Config file: ", kind="file")
birthday = askr.ask_date("Birthday: ", ("%Y-%m-%d", "%d.%m.%Y"), max_date=date.today())
alarm = askr.ask_time("Alarm: ", ("%H:%M", "%I:%M %p"))

# Safety check before something dangerous
if askr.ask_confirm_text("Type 'delete' to remove all data: ", "delete"):
    ...

Options available on (almost) every prompt

# default: Enter returns it, and the prompt shows it
port = askr.ask_int("Port: ", default=8080)          # Port [8080]:

# validator + error: your own rule on top of the built-in checks
even = askr.ask_int("Even number: ", validator=lambda n: n % 2 == 0, error="Must be even.")

# max_attempts: give up instead of asking forever
try:
    code = askr.ask_int("Code: ", max_attempts=3)
except askr.TooManyAttemptsError:
    print("Too many wrong attempts.")

Translating and rewording

import dataclasses
import askr

askr.set_messages(askr.Messages.german())            # complete German translation

# or change single texts
askr.set_messages(dataclasses.replace(askr.get_messages(), empty="Please type something."))

Colors

Error messages are red when the output is a terminal. They stay plain if the NO_COLOR environment variable is set. Force it with askr.set_color(True) or askr.set_color(False).

Good to know

  • Mistakes by the programmer (for example min_value greater than max_value) raise a ValueError immediately instead of asking the user forever.
  • Ctrl+C and Ctrl+D are not caught, so your program can handle KeyboardInterrupt and EOFError the way it wants.

License

MIT

Release files for askr 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for askr 0.2.0
File Size Uploaded
askr-0.2.0.tar.gz 20.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for askr 0.2.0
File Interpreter ABI Platform
askr-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 36.2 kB

Release files / askr-0.2.0.tar.gz

Download URL askr-0.2.0.tar.gz
Size 20.4 kB
Tags Source
SHA-256 checksum
How to use checksums
22e970475c4f0c99c828303a4ec44d22420301a34cd0c99de0042794e7857302
BLAKE2b-256 checksum
How to use checksums
426fc06917639a626a1e79ba5b2712ab7c84812ce4b563ab1859f15439033f39
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Release files / askr-0.2.0-py3-none-any.whl

Download URL askr-0.2.0-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7d5bfcf46ab61ac448018fbd7498b67b8676462a8ca8c5866755210e94ca855c
BLAKE2b-256 checksum
How to use checksums
8e3d23661fbd18a96f3e09264ca54e459a8756a59919c9424f2cb362aaae9ea4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 27, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page