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

python
print("Welcome to DevBrainBox!")

Output

text
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.

python
name = "Alice"
age = 22

print("Name:", name)
print("Age:", age)

Output

text
Name: Alice
Age: 22

Printing Variables

print() can display the value stored in a variable. This is useful because a variable's value may change during execution.

python
course = "Python"
duration = 30

print(course)
print(duration)

Output

text
Python
30

What 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.

python
name = input("Enter your name: ")

print("Hello,", name)

Sample Output

text
Enter your name: John
Hello, John

The 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.

python
age = input("Enter your age: ")

print(type(age))

Output

text
<class 'str'>

To perform arithmetic, convert the returned text to an appropriate numeric type.

Converting User Input

Convert to Integer

python
age = int(input("Enter your age: "))

print(age + 5)

Sample Output

text
Enter your age: 20
25

int() converts suitable entered text into an integer. Invalid text raises ValueError, which can be handled with exception handling.

Convert to Float

python
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.

python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

total = num1 + num2

print("Sum =", total)

Sample Output

text
Enter first number: 15
Enter second number: 25
Sum = 40

Formatted Output

Using Commas

python
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.

python
name = "Emma"
marks = 95

print(f"{name} scored {marks} marks.")

Output

text
Emma scored 95 marks.

Escape Characters

Escape characters add special formatting or include characters that would otherwise affect string syntax.

New Line

python
print("Python\nProgramming")

Output

text
Python
Programming

Tab Space

python
print("Name\tAge")
print("John\t25")

Output

text
Name    Age
John    25

Double Quotes Inside a String

python
print("She said, \"Python is easy!\"")

A Simple Interactive Program

This example combines input and output to create a small interactive experience.

python
name = input("Enter your name: ")
city = input("Enter your city: ")

print(f"Hello {name}!")
print(f"You live in {city}.")

Sample Output

text
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.
Let's learn with DevBrainBox AI