🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreeHow to Use Input Function in Python

A calculation in Python may need a number read while the program runs. Python’s input() reads one line from standard input and returns it as text.
This article explains how to convert numeric text and check what happens when the input has the wrong format.
TL;DR
Python’s input() reads a line as text, so convert it before using it as a number.
- Use a prompt to tell the user what to enter.
- Use int() or float() for numeric text.
- Use split() when one line contains several values.
What Is Python input()?
input() pauses for one line from standard input and returns that line as a string, with its trailing newline removed. Its syntax is input(prompt), where prompt is optional text shown before the read.
I use a clear prompt because input() cannot infer the expected format. The response is text, so the program must parse it before numeric calculations.
Standard input can be connected to a terminal, a pipe, or a redirected file. The same call reads from that stream, which lets a script use supplied lines without a person typing each response. Repeated calls consume successive lines in order, so redirected input must provide enough lines for the program’s prompts.
How to Use input() Step by Step
The following file reads a name and greets the person who entered it. I ran it with Ada as the response.
Step 1: Read a string from the user
Save input() to a variable when you need the response later. I ran python3 input_demo.py with the line Ada and got the output shown below.
name = input("Name: ")
print("Hello,", name)
Output:
Name: Hello, Ada

Step 2: Convert numeric input
A yes-or-no answer needs an explicit comparison with accepted words such as yes or no. Calling bool() on the response is misleading because both “True” and “False” are non-empty strings and therefore truthy.
The value “-2” converts successfully to an integer, even if a quantity prompt should reject it. Check application limits after parsing and state the accepted range when requesting another answer.
Step 3: Read two values on one line
Call split() to divide a line at whitespace, then map(int, …) converts each token. I ran this file with 6 7 as its input.
a, b = map(int, input("Two integers: ").split())
print("Product:", a * b)
Output:
Two integers: Product: 42

The assignment needs exactly two values, one for each variable.
str.split() without an argument separates on runs of whitespace, so multiple spaces or tabs still yield separate tokens. It ignores whitespace at the ends, but does not parse quoted phrases as a shell would.
When the number of entries is not fixed, a loop can read one line per entry and stop at a defined condition.
Step 4: Pass input to a function
A function can call input() itself, but accepting an argument makes its calculation usable with values from other sources.
def square(number):
return number * number
value = int(input("Number: "))
print(square(value))
I ran python3 square_demo.py with 5 as input. Output:
Number: 25

Input Errors and Limits
The table pairs common input failures with the response that handles each one.
| Case | What happens | What to do |
|---|---|---|
| Text such as hello passed to int() | ValueError | Check the format or catch the exception and explain the expected number |
| Two numbers passed whole to int() | ValueError because the full line is not one integer | Split the line and convert each token |
| Input stream ends before a line arrives | EOFError | Provide input or handle end-of-file |
| Blank line converted to int() | ValueError | Check for empty text before conversion |
Conclusion
Choose the input shape first: one response, converted numeric text, or several split values. Handle invalid and exhausted input at the point where the program reads it.
- Python input() documentation
- Python string functions
- How to Split a String in Python
- Hide password input with getpass
FAQ
These answers cover common ways of reading and using input.
Does input() return a number?
No. input() returns a string. Convert numeric text with int() or float().
How do I read two numbers on one line?
Split the input line on whitespace and convert each token, for example with map(int, input().split()).
Can input() be used inside a function?
Yes. It can be called inside a function, though collecting input outside and passing a value as an argument makes the function reusable.


