Definition
📌 Input & Output
Python's print() function outputs text to the console, and input() reads a line of text typed by the user.
Syntax
syntax
print(value1, value2, ...)
variable = input("prompt")Example
py
age_text = input("Enter your age: ")
age = int(age_text)
print("You are", age, "years old.")Output
outputEnter your age: 20
You are 20 years old.
Important Points
input()always returns astr, regardless of what the user types.- Use
int()orfloat()to convert input text into a number when needed. print()can take multiple values separated by commas, automatically adding spaces between them.print(f"...{variable}...")(f-strings) is the modern, readable way to build formatted output.
Common Mistakes
- Trying to do math directly on the result of
input()without converting it first. - Forgetting that an invalid conversion, like
int("abc"), raises aValueError. - Confusing
print()'s comma-separated arguments with string concatenation.
Interview Tip
🎯 Before your interview
"Why does input() always return a string?" — because Python can't know in advance if the user meant to type a number, a word, or something else; you decide the intended type.
