CodingNic

Python Foundations

User Input

Python Foundations 18 min read

User Input

User Input

Many programs become useful when they interact with the user. Python allows you to collect information from the keyboard using the input() function.

What Is User Input?

The input() function pauses the program and waits for the user to type something.

python
name = input("Enter your name: ")
print(name)

How input() Works

The text inside input() is called a prompt. It tells the user what to enter.

python
city = input("Enter your city: ")
print("You live in", city)

Input Is Stored as Text

By default, everything entered with input() is stored as a string.

python
age = input("Enter your age: ")
print(type(age))

Output:

text
<class 'str'>

Converting Input

Use int() or float() when working with numbers.

python
age = int(input("Enter your age: "))
print(age + 1)
python
price = float(input("Enter price: "))
print(price * 2)

Multiple Inputs

You can collect several values in one program.

python
name = input("Enter name: ")
age = int(input("Enter age: "))
country = input("Enter country: ")

print(name)
print(age)
print(country)

Code Along

python
name = input("What is your name? ")
favorite_food = input("What is your favorite food? ")

print("Hello", name)
print("You like", favorite_food)

Mini Challenge

Create a program that asks the user for:

  • Name
  • Age
  • Favorite color

Then print all answers nicely.

Real World Use Case

Login forms, registration pages, surveys, calculators, and search tools all rely on user input.

Quiz

  1. What function is used to collect input?
  2. What type does input() return by default?
  3. Why do we use int() with age?
  4. What is a prompt?

Assignment

Build a simple profile app that asks for a user’s name, age, country, and hobby, then displays the information.

Summary

You learned how to collect user input, use prompts, convert input into numbers, and build interactive Python programs.