Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Python's input() function pauses program execution, displays an optional prompt message, and reads a line of text typed by the user from the keyboard. Regardless of whether the user types a name, a number, or a decimal value, input() always returns a string (str), which you can convert to int or float for calculations.
In earlier lessons, every variable value was hardcoded directly inside your Python file. Real software—from ATM machines and billing counters to quiz apps and admission forms—must respond to live user data. Mastering input() turns static scripts into interactive applications.
input() Works Step by StepWhen Python encounters input("prompt"), four things happen in sequence:
str object.Sample Terminal Run:
": ") at the end of your prompt string so the user's cursor does not stick right against the colon.input() Always Returns a String (str)A very common surprise for beginners happens when you ask the user for numbers and try to add them immediately:
Because both num1 and num2 are strings ("10" and "20"), the + operator joins them together into "1020" instead of adding them mathematically to get 30.
int and float)To perform arithmetic, wrap input() inside int() for whole numbers or float() for decimal numbers:
| Desired Data Type | Conversion Function | Example User Input | Resulting Python Value |
|---|---|---|---|
| Text (name, email, city) | None needed (str) | Priya Sharma | "Priya Sharma" |
| Whole number (age, qty) | int(input(...)) | 21 | 21 (int) |
| Decimal (price, weight) | float(input(...)) | 499.50 | 499.5 (float) |
| Clean trimmed text | input(...).strip() | " Delhi " | "Delhi" (str) |
Users frequently type accidental leading/trailing spaces or inconsistent capitalization. Chain .strip() and .lower() or .title() right on input() to normalize data cleanly:
.split()In competitive programming and quick CLI tools, you can accept multiple values on a single line using .split():
input() Valuesint()If a user enters "12.5", calling int("12.5") raises a ValueError. If decimals are possible, convert using float() first (or keep it as a float):
input NameNever name a variable input = "hello". Doing so shadows Python's built-in input() function and causes TypeError: 'str' object is not callable later in your script. Use descriptive names like user_input or raw_answer.
2006) and prints their approximate age in 2026.item_name, unit_price, and quantity, computes an 18% GST amount, and prints a formatted final bill.first_name last_name on a single line, split it into two variables, and print a suggested username like first.last@mskinstitute.in in lowercase.input() function always return?5 and 7 for the code below?.strip() commonly chained with input()?int("45.8") in Python?Now that you can capture and convert live user input, continue to Data Types Overview → to explore all built-in Python data types in depth.
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Constants (Python Convention) | Data Types Overview |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Go beyond self-study with 46 chapters, 236 practical topics, 14 portfolio projects, code reviews, and a verifiable certificate at MSK Institute Shikohabad or Live Online.