← Back to Chapters

Python User Input

⌨️ Python User Input

⚡ Quick Overview

In Python, you can ask the user to type something while the program is running using the built-in input() function…

User input is essential for making your programs interactive—for example, forms, quizzes, calculators, and games.

? Key Concepts

  • input() returns a string – even if the user types numbers.
  • Type conversion – use int() or float() to convert the string to numbers.
  • Prompt message – the text inside input("...") explains what the user should type.
  • Multiple values – combine input() with split() to read more than one value at a time.
  • Formatting output – use + with strings or f-strings like f"Hello {name}".

? Syntax and Theory

Basic syntax of the input() function:

? View Basic input() Syntax
# store what the user types in a variable
variable_name = input("Prompt message shown to the user: ")

To use a value as an integer or float, wrap input() with a type conversion function:

? View Type Conversion Syntax
# read age as an integer
age = int(input("Enter your age: "))
# read height as a float
height = float(input("Enter your height in meters: "))

? Code Examples

? Basic Text Input

Ask the user for their name and greet them:

? View Code Example
# ask for the user's name
name = input("Enter your name: ")
# greet the user
print("Hello, " + name)

? Numeric Input with int()

Read an age as a number so you can perform numeric operations:

? View Code Example
# read the age as text
age = input("Enter your age: ")
age = int(age)  # convert string to integer
# print the result
print(f"You are {age} years old.")

➕ Reading Multiple Values with split()

Read two numbers separated by a space and print their sum:

? View Code Example
# read two numbers at once
a, b = input("Enter two numbers separated by space: ").split()
# convert them to integers
a = int(a)
b = int(b)
# print their sum
print("Sum =", a + b)

? Reading Floating-Point Numbers

Read a decimal value such as height and format the output:

? View Code Example
# input float value
height = float(input("Enter your height in meters: "))
# show with 2 decimals
print(f"Your height is {height:.2f} meters")

? Live Output and Explanation

?️ What Happens When You Run These Programs?

  • For the name example:
    • Program shows: Enter your name:
    • User types: Alex
    • Output: Hello, Alex
  • For the age example:
    • User enters: 18
    • input() returns "18"
    • int("18")18
    • Output: You are 18 years old.
  • For the two-number example:
    • User types: 5 7
    • split() → ["5", "7"]
    • Sum → 12
  • For the height example:
    • User types: 1.735
    • float() converts to 1.735
    • Formatted: 1.73

? Tips and Best Practices

  • Always convert input before calculations.
  • Use split() for multiple inputs.
  • Use clear prompt messages.
  • Prefer f-strings for readability.
  • Validate input in real applications.

? Try It Yourself

  • Write a program to multiply two numbers.
  • Ask name + age and print: “Hello ___, you are ___ years old.”
  • Ask for numbers and print their average.
  • Create a BMI calculator.