Python makes it easy to interact with users by taking input directly from the keyboard. Whether you’re building a simple calculator, a login system, or a data processing tool, understanding how to handle user input is essential.
In this tutorial, we’ll cover:
✅ How the input()
function works
✅ Storing user input in variables
✅ Type conversion (string to int, float, boolean)
✅ Practical examples for better understanding
1. How the input()
Function Works
The input()
function in Python allows users to enter data from the keyboard. By default, it always returns a string, even if the user enters a number.
Basic Syntax:
user_input = input("Enter something: ")
print(user_input)
Example:
name = input("Enter your name: ")
print("Hello, " + name)
Output:
Enter your name: Yogendra
Hello, Yogendra
2. Storing User Input in Variables
Since input()
returns a string, we can store it in a variable for later use.
Example:
age = input("Enter your age: ")
print("Your age is:", age)
Output:
Enter your age: 25
Your age is: 25
⚠️ Note: Even if you enter a number, input()
treats it as a string.
3. Converting Input to Different Data Types
Since input()
always gives a string, we often need to convert it to other data types like integers, floats, or booleans.
A. Converting to Integer (int()
)
num = input("Enter a number: ") # Returns string
converted_num = int(num) # Converts to integer
print(converted_num + 10) # Now we can do math
Output:
Enter a number: 5
15
❌ Error Case:
If the user enters a non-number (e.g., “five”), Python will raise a ValueError
.
✅ Solution: Use error handling (try-except) in real applications.
B. Converting to Float (float()
)
price = float(input("Enter price: "))
print("Total with tax:", price * 1.18) # Adds 18% tax
C. Converting to Boolean (bool()
)
answer = bool(input("Enter True/False: "))
print(answer) # Empty input = False, any input = True
4. Practical Example: Adding Two Numbers
Let’s create a program that takes two numbers from the user and adds them.
Code:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
sum = num1 + num2
print("Sum:", sum)
Output:
Enter first number: 10
Enter second number: 20
Sum: 30
5. Taking Multiple Inputs in One Line
You can use split()
to take multiple inputs at once.
Example:
x, y = input("Enter two numbers (separated by space): ").split()
x = int(x)
y = int(y)
print("Product:", x * y)
Output:
Enter two numbers (separated by space): 5 4
Product: 20
6. Best Practices for User Input
✔ Always validate input (check if it’s a number, etc.)
✔ Use clear prompts (e.g., “Enter age (numbers only): “)
✔ Handle errors (try-except blocks for invalid inputs)
Conclusion
Mastering input()
in Python is crucial for building interactive programs. Remember:
🔹 input()
always returns a string.
🔹 Use int()
, float()
, or bool()
for conversions.
🔹 Handle errors to make your programs robust.
Next Steps:
- Learn about conditional statements (
if-else
) to validate input. - Explore loops (
while
,for
) for repeated input prompts.
This blog complements the video tutorial, providing a written reference for better understanding. Happy coding! 🐍💻