Introduction
Once you’ve mastered how to display output using the print()
statement, the next natural step is making your program interactive. That’s where the input()
function comes in. It lets users type something, and your code can respond to it.
In this article, we’ll cover what input()
does, how to use it properly, and what you need to keep in mind as a beginner.
Why Is Input Important in Programming?
So far, you’ve been writing code that prints fixed values to the screen. While that’s a great way to start, real programs rarely work that way. Most of the time, users are involved.
Think of apps where you type your name, click a button, or enter your age to get a result. Behind all of that, some form of input handling is happening.
Python’s input()
function is your first step into building these types of programs.
Understanding the input()
Function
The input()
function pauses your program and waits for the user to type something. Whatever the user enters is returned as a string.
Here’s the basic idea:
name = input("What's your name? ")
print("Hello,", name)
Let’s break this down:
- The text inside
input()
is the prompt that appears to the user. - Whatever the user types is saved into the variable
name
. - You can then use
print()
to respond using that input.
Example: Building a Simple Interactive Program
Let’s say you want to ask someone for their favorite color and respond with a message.
color = input("What’s your favorite color? ")
print("Nice choice! I like", color, "too.")
When this program runs:
- The user sees the question.
- They type their answer.
- Your code responds immediately.
Keep in Mind: Input Returns Strings
No matter what the user types, Python treats it as text (a string). So if you want to work with numbers, you’ll need to convert the input.
age = input("How old are you? ")
print("You will be", age, "next year.")
In the above case, age
is a string. If you try math on it, you’ll get unexpected results. To fix that:
age = int(input("How old are you? "))
print("Next year, you will be", age + 1)
Practice Task
Try writing a program that asks for:
- Your name
- Your city
- Your favorite number
Then display a personalized sentence using that information.
Watch the Lesson on YouTube (Starts at 26:54)
You can follow along with this part of our course video where we explain the input()
function with real examples.
🎥 Watch here:
Python Input Function – Video Lesson (26:54)
What’s Next?
Now that you know how to take input, we’ll explore how Python handles different data types—text, numbers, and more. Understanding data types is key to building working programs.
Want the Full Training?
This blog is part of a complete beginner-friendly Python course that covers everything from installation to Object-Oriented Programming.
👉 Join the Full Course
Explore more resources at qaonlinetraining.com, including free practice exercises and real-time help.