Codectionary / Developer documentation / Python

input()

input() reads a line of text typed by the user from the console and returns it as a string. It optionally accepts a prompt string to display before waiting for input. Since input() always returns a string, converting the result to a number or other type is a common next step when working with numeric input.

Syntax

variable = input(prompt)

Examples

Basic Input

Prompting the user and reading their response.

name = input("What is your name? ")
print(f"Hello, {name}!")

Converting Input to a Number

Since input() always returns a string, convert it before doing arithmetic.

age_text = input("Enter your age: ")
age = int(age_text)

print(f"Next year you will be {age + 1}")

Input Validation Loop

Repeating a prompt until the user provides valid input.

while True:
    value = input("Enter a positive number: ")
    if value.isdigit():
        number = int(value)
        break
    print("That wasn't valid, try again.")

print(f"You entered {number}")

Best practices

  • Always convert input() results with int(), float(), etc. before doing arithmetic - they arrive as strings
  • Wrap numeric conversions in try/except to handle invalid input gracefully rather than letting the program crash
  • Use a clear, specific prompt string so the user knows exactly what format is expected
  • Validate input in a loop rather than accepting the first (possibly invalid) response silently

At a glance

Purpose
Scripting and general-purpose applications
File extension
.py
Runs in
Python interpreter
Usually used with
Python standard library and packages

Specifications & further reading

Related Python documentation

True
In Python, True is a built-in constant that represents the boolean value of true. Alongside False, True enables logical and conditional operations. It's a fundamental keyword used throughout Python programming for decision-making, loops, comparisons, and boolean logic. True plays a key role in if statements, while loops, and logical expressions.
False
False is Python's built-in constant representing the boolean value false. It's the logical opposite of True and is essential for conditional logic, loop control, and boolean operations. False is used to indicate negative conditions, failed validations, or when something is not the case. Understanding False is crucial for writing effective control flow in Python programs.
None
None is Python's built-in singleton value that represents the absence of a value or a null result. It is its own type (NoneType) and is commonly used as a default parameter value, a placeholder before a variable is assigned real data, or a function's implicit return value when nothing is explicitly returned. Unlike 0, an empty string, or an empty list, None specifically means 'no value here', not 'an empty value'.
Variables & Assignment
Variables in Python are names bound to values, created the moment you first assign to them - there's no need to declare a type in advance, since Python is dynamically typed. A single variable can be reassigned to a completely different type later in the program. Python also supports several convenient assignment patterns, including assigning multiple variables at once and using augmented assignment operators.