Python Input & Output
Read user input and display useful formatted output.
Every program communicates with users in two ways: it accepts input and displays output. A calculator asks for numbers and shows a result, while a login system asks for credentials before displaying a success or error message.
Python makes input and output simple. In this lesson, you will learn how to display information, accept user input, convert entered values, and format output so it is easy to read.
What is Output?
Output is information that a program displays to the user. Python's print() function shows text, numbers, variables, and calculation results on the screen.
Displaying Text
print("Welcome to DevBrainBox!")Output
Welcome to DevBrainBox!The text inside quotation marks is a string, and print() displays it exactly as written.
Printing Multiple Values
Display more than one value by separating them with commas. Python adds a space between comma-separated values by default.
name = "Alice"
age = 22
print("Name:", name)
print("Age:", age)Output
Name: Alice
Age: 22Printing Variables
print() can display the value stored in a variable. This is useful because a variable's value may change during execution.
course = "Python"
duration = 30
print(course)
print(duration)Output
Python
30What is Input?
Input is information that a user provides to a program. When Python reaches input(), it displays an optional prompt, pauses, and waits for the user to enter text.
name = input("Enter your name: ")
print("Hello,", name)Sample Output
Enter your name: John
Hello, JohnThe message passed to input() is called a prompt. It tells the user what information to enter.
Input is Stored as a String
input() always returns a string, even when the user types digits.
age = input("Enter your age: ")
print(type(age))Output
<class 'str'>To perform arithmetic, convert the returned text to an appropriate numeric type.
Converting User Input
Convert to Integer
age = int(input("Enter your age: "))
print(age + 5)Sample Output
Enter your age: 20
25int() converts suitable entered text into an integer. Invalid text raises ValueError, which can be handled with exception handling.
Convert to Float
price = float(input("Enter product price: "))
print(price)float() is useful for decimal values such as prices or measurements.
Performing Calculations with User Input
Once entered values are converted to numbers, they can be used in calculations.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
total = num1 + num2
print("Sum =", total)Sample Output
Enter first number: 15
Enter second number: 25
Sum = 40Formatted Output
Using Commas
name = "Emma"
marks = 95
print("Student:", name, "Marks:", marks)Using f-Strings
An f-string is a modern and readable way to insert variables and expressions into text.
name = "Emma"
marks = 95
print(f"{name} scored {marks} marks.")Output
Emma scored 95 marks.Escape Characters
Escape characters add special formatting or include characters that would otherwise affect string syntax.
New Line
print("Python\nProgramming")Output
Python
ProgrammingTab Space
print("Name\tAge")
print("John\t25")Output
Name Age
John 25Double Quotes Inside a String
print("She said, \"Python is easy!\"")A Simple Interactive Program
This example combines input and output to create a small interactive experience.
name = input("Enter your name: ")
city = input("Enter your city: ")
print(f"Hello {name}!")
print(f"You live in {city}.")Sample Output
Enter your name: Rahul
Enter your city: Delhi
Hello Rahul!
You live in Delhi.Common Mistakes
- Forgetting quotation marks around literal text in print().
- Trying to add numeric input without converting it to int or float.
- Using variable names that are difficult to understand.
- Forgetting to close quotation marks or parentheses.
Paying attention to these details helps avoid errors and produces cleaner programs.
Key Takeaways
- Output is information displayed with print().
- Input is information entered by a user with input().
- input() always returns a string.
- Use int() or float() to convert suitable input for calculations.
- F-strings provide a clean and readable way to format output.
- Escape characters such as \n, \t, and \" help format text.
- Input and output are essential building blocks for interactive Python programs.