Take Input in Python
Python is one of the most popular programming languages for beginners, students, and professional developers. One of the first concepts you need to understand when creating interactive Python programs is how to take input in Python. User input allows a program to receive information while it is running instead of depending only on fixed values written in the source code.
For example, a Python program can ask a user to enter a name, age, marks, salary, or any other value and then use that information for calculations or display. Input handling is commonly used in command-line applications, programming exercises, automation scripts, and beginner-level Python projects.
Python provides a built-in input() function for accepting keyboard input. One important point to remember is that in Python 3, the input() function returns the entered value as a string. When numerical calculations are required, the input must be converted into the appropriate data type using functions such as int() or float().
In this guide, you will learn how to take string, integer, and decimal input in Python, accept multiple values in one line, take list input, use loops for repeated input, validate user data, and understand the difference between Python 2’s raw_input() and Python 3’s input().
Table of Contents

Complete Advance AI Topics: Click Here
SQL Tutorial: Click Here
What Is User Input in Python?
User input is information provided by a user while a program is executing. Instead of assigning a fixed value to a variable, the program asks the user to enter the required information.
For example:
name = input("Enter your name: ")
print("Hello,", name)
When this program runs, Python waits for the user to enter a name. If the user enters Akshay, that value is stored in the name variable and then displayed.
This makes the program interactive because its output can change depending on the information supplied by the user.
Understanding the input() Function
The input() function is a built-in Python function that reads a line entered through the keyboard. You can provide an optional prompt to tell the user what information should be entered.
Syntax
input(prompt)
The prompt is optional. It is displayed before Python waits for the user’s response.
Simple Example
name = input("Enter your name: ")
print(name)
If the user enters:
Rahul
Python stores "Rahul" as a string in the name variable.
Important Characteristics of input()
Before using user input in larger programs, keep the following points in mind:
input()waits for the user to enter data and press Enter.- In Python 3, the returned value is a string.
- You can provide a prompt to guide the user.
- Numeric input normally needs type conversion before calculations.
- Invalid input can be handled with validation and exception handling.
How to Take String Input in Python
Taking a string as input is the simplest use of the input() function. Since names and other text values are already strings, no conversion is required.
name = input("Enter your name: ")
print(f"Hello, {name}!")
Example Output
Enter your name: Devansh
Hello, Devansh!
The value entered by the user is stored as text and can be directly used with string operations.
How to Take Integer Input in Python
A common beginner question is how to take integer input in Python. Because input() initially returns a string, you can use the int() function to convert the entered value into an integer.
age = int(input("Enter your age: "))
print("Your age is:", age)
If the user enters 21, the value stored in age is an integer. You can then perform mathematical operations on it.
age = int(input("Enter your age: "))
print("After 5 years:", age + 5)
For an input of 21, the output will be:
After 5 years: 26
How to Take Float Input in Python
When a program needs a decimal number, use the float() function with input().
marks = float(input("Enter your marks: "))
print("Marks:", marks)
If the user enters 89.5, Python converts the entered text into a floating-point number that can be used for calculations.
Taking Different Data Types as Input
A single Python program can accept different types of information and convert each value according to its requirement.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
marks = float(input("Enter your marks: "))
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Marks: {marks}")
Example Output
Enter your name: Johnson
Enter your age: 21
Enter your marks: 89.5
Name: Johnson
Age: 21
Marks: 89.5
In this example, the name remains a string, the age is converted into an integer, and the marks are converted into a floating-point value.
How to Take Multiple Inputs in Python in One Line
Sometimes a program needs multiple values from the user on a single line. Python’s split() method can separate the entered values.
a, b = input("Enter two values: ").split()
print("First:", a)
print("Second:", b)
If the user enters 10 20, Python separates the two values using whitespace. The values are initially strings.
For integer values, combine split() with map():
a, b = map(int, input("Enter two numbers: ").split())
print("Sum:", a + b)
For input 10 20, the output is:
Sum: 30
How to Take Multiple Integer Inputs in Python
When several integer values need to be entered on one line, you can convert all the values into integers and store them in a list.
numbers = list(map(int, input("Enter numbers: ").split()))
print(numbers)
If the user enters:
10 20 30 40 50
The resulting list is:
[10, 20, 30, 40, 50]
Here, input() receives the complete line, split() separates the values, map() applies int() to each value, and list() stores the converted results.
How to Take List Input in Python
Python does not provide a separate function specifically for taking list input. Instead, you can take a line of input and use split() to create individual elements.
items = input("Enter items: ").split()
print(items)
If the user enters:
Python Java PHP
The output will be:
['Python', 'Java', 'PHP']
For a list containing integers, use map() and int():
numbers = list(map(int, input("Enter numbers: ").split()))
print(numbers)
This method is especially common in programming exercises where an array or list is provided as space-separated input.
Taking Input Using a for Loop
A for loop is useful when the program knows how many values need to be collected.
numbers = []
for i in range(5):
number = int(input("Enter a number: "))
numbers.append(number)
print("Numbers:", numbers)
This program asks the user to enter five numbers and stores them in a list. It is easy to understand and useful when the required number of inputs is fixed.
Taking Input Repeatedly Using a while Loop
When the number of inputs is not known in advance, a while loop can continue accepting values until a specific condition is reached.
numbers = []
while True:
value = input("Enter a number or type stop: ")
if value.lower() == "stop":
break
numbers.append(int(value))
print("Numbers:", numbers)
The program continues taking numbers until the user enters stop. This approach can be useful for interactive command-line programs.
Input Validation in Python
Users may not always enter the type of information expected by a program. For example, if a program expects an integer and the user enters text, using int() can result in a ValueError.
To handle such situations, Python provides try-except.
Example of Input Validation
try:
age = int(input("Enter your age: "))
print(f"Your age is: {age}")
except ValueError:
print("Invalid input! Please enter a valid integer.")
Instead of terminating unexpectedly, the program displays a helpful message when the entered value cannot be converted into an integer.
Using while with try-except for Valid Input
If you want the program to continue asking until a valid number is entered, combine a while loop with exception handling.
while True:
try:
age = int(input("Enter your age: "))
break
except ValueError:
print("Please enter a valid integer.")
print("Your age is:", age)
This approach provides a better interactive experience because an invalid value does not immediately terminate the program.
The raw_input() Function in Python 2
Older Python 2 programs used raw_input() to receive user input as a string.
name = raw_input("Enter your name: ")
print(name)
The raw_input() function is not available in Python 3. Modern Python programs use input() instead.
Python 2 also handled input() differently. In Python 2, input() evaluated the entered data as a Python expression, while raw_input() returned the entered text. This difference mainly matters when working with older Python code.
Best Practices for Taking Input in Python
Following a few simple practices can make input-handling code clearer and more reliable.
1. Use Clear Prompts
Tell users exactly what information they need to provide.
name = input("Enter your full name: ")
2. Convert Values Explicitly
Use the appropriate conversion function when a value is expected to be an integer or decimal number.
quantity = int(input("Enter quantity: "))
price = float(input("Enter price: "))
3. Validate User Input
Use try-except when invalid input could cause the program to stop unexpectedly.
4. Remember That input() Returns a String
This is one of the most important concepts for Python beginners.
value = input("Enter a number: ")
print(type(value))
If the user enters 100, the output will still be:
<class 'str'>
Therefore, numerical input should be converted before performing arithmetic operations.
Practical Example: Simple Calculator
The following example combines user input, type conversion, arithmetic operations, and basic validation.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
print("Addition:", num1 + num2)
print("Subtraction:", num1 - num2)
print("Multiplication:", num1 * num2)
if num2 != 0:
print("Division:", num1 / num2)
else:
print("Division by zero is not allowed.")
This example shows how user-provided values can be converted into numbers and then used in a practical Python application.
Conclusion
Understanding how to take input in Python is an essential skill for anyone learning Python programming. The built-in input() function provides a simple way to make programs interactive and allows users to provide information while the program is running.
The most important point to remember is that input() returns a string in Python 3. When numerical calculations are required, use int() or float() to convert the entered value. For multiple values, split() and map() provide a convenient solution, while for and while loops can be used when input needs to be collected repeatedly.
Input validation is also important because users may provide unexpected information. Using try-except and suitable validation logic can make Python programs more reliable and easier to use.
YT:- DecodeIT
Frequently Asked Questions (FAQ)
1. How do you take input in Python?
Use the built-in input() function:
name = input("Enter your name: ")
print(name)
2. What does input() return in Python?
In Python 3, input() returns the entered value as a string. If another data type is required, convert the value explicitly.
3. How do I take integer input in Python?
Use int() together with input():
number = int(input("Enter a number: "))
4. How do I take float input in Python?
Use the float() function:
number = float(input("Enter a decimal number: "))
5. How do I take multiple inputs in Python in one line?
Use split() to separate values:
a, b = input().split()
For integer values, use:
a, b = map(int, input().split())
6. How do I take list input in Python?
For space-separated integers, use:
numbers = list(map(int, input().split()))
7. How do I take string input in Python?
Use input() directly:
text = input("Enter a string: ")
8. Why does input() return a string?
The Python 3 input() function reads keyboard input as text. If your program needs an integer or floating-point value, convert the string using int() or float().
9. Is raw_input() available in Python 3?
No. raw_input() was used in Python 2. In Python 3, the standard function for receiving user input is input().
10. How can I handle invalid input in Python?
You can use try-except to handle conversion errors:
try:
number = int(input("Enter a number: "))
except ValueError:
print("Please enter a valid number.")
11. Can I take input repeatedly in Python?
Yes. You can use a for loop when the number of inputs is fixed or a while loop when input should continue until a condition is met.
12. What is type casting in Python input?
Type casting means converting input from one data type to another. For example, int(input()) converts entered text into an integer, while float(input()) converts it into a floating-point number.
13. How can I take a list of strings from the user?
Use split():
items = input("Enter items: ").split()
This separates space-separated values and stores them as strings in a list.
Related Keywords
Take Input in Python, how to take input in Python, taking integer input in Python, Python input function, input() function in Python with example, how to take string input in Python, how to take float input in Python, how to take multiple inputs in Python in single line, how to take multiple integer input in Python in single line, how to take list input in Python, how to take array input in Python in single line, Python user input, Python type casting, Python input validation, for loop in Python, while loop in Python, Python programming examples, online Python compiler.