Python Input and Output


Working with input and output is a core part of Python programming. Almost every program needs a way to accept information from a user and display something back on the screen. Python makes this simple with two main tools: the input() function for reading data and the print() function for showing results. In this chapter, you’ll learn how these functions work, how to handle different types of input and how to format your output so it looks clean and readable.

What Is Input and Output in Python?

Input refers to any data your program receives while it is running. Output is the information your program sends back to the screen. A simple calculator, for example, needs input from the user in the form of numbers and then prints the answer as output.

Python gives you a straightforward way to do both. The input() function reads text from the user, and the print() function shows text or values on the screen. Even though they look simple, learning how to use them correctly forms the foundation of building real applications.

Getting Input with input()

The input() function reads a line of text typed by the user. It always returns a string, no matter what the user enters. This is an important point because if the user types a number, Python still treats it as text until you cast it.

Basic example:

name = input("Enter your name: ")
print("Hello", name)

The text inside the parentheses is optional, but adding a message tells the user what to type. When the user presses Enter, the value is returned and stored in the variable.

Why input() Always Returns a String

Since all input comes in as text, you often need to convert it before using it in calculations.

For example:

num1 = input("Enter a number: ")
total = num1 + 10   # This will not work

Here, Python will not add 10 to num1 because it is a string. To fix it:

num1 = int(input("Enter a number: "))
total = num1 + 10
print(total)

Casting is essential whenever you expect numeric input.

Reading Multiple Inputs

Sometimes you need more than one value from the user. You can call input() multiple times:

first = input("First name: ")
last = input("Last name: ")
print("Full name:", first, last)

Or you can accept multiple inputs in one line using split():

a, b = input("Enter two numbers: ").split()

This reads both values as strings. You can cast them if needed:

a, b = map(int, input("Enter two numbers: ").split())

Displaying Output with print()

The print() function is used to show results, messages, or variable values. You can print text, numbers, lists, or even entire dictionaries.

A simple example:

print("Welcome to Python")

You can also print multiple items separated by commas:

x = 10
y = 20
print("Values are:", x, y)

Python adds a space between the items when commas are used.

Printing Without a New Line

By default, Python’s print adds a new line at the end. If you want to continue printing on the same line, use the end parameter.

Example:

print("Loading", end=" ")
print("Please wait")

Output:

Loading Please wait

This helps when creating progress messages or formatted output.

Using sep to Control Spacing

The sep parameter controls how items are separated.

Example:

print("2025", "01", "10", sep="-")

Output:

2025-01-10

This is useful for formatting dates, IDs, or combined strings.

Formatting Output with f-strings

F-strings make it easy to show variables inside text without complicated syntax.

Example:

name = "Aarohi"
age = 22
print(f"My name is {name} and I am {age} years old.")

F-strings are clean, fast, and widely used in real projects.

Handling Numbers in Output

When printing numbers, you may want to format decimal places. Python makes this easy.

Example:

price = 99.4567
print(f"Final price: {price:.2f}")

Output:

Final price: 99.46

You can format integers, percentages, or large numbers the same way.

Taking Input Safely

Sometimes users might type something unexpected. To avoid errors, you can check the value before casting.

Example:

value = input("Enter a number: ")

if value.isdigit():
    value = int(value)
    print("You entered:", value)
else:
    print("Invalid input")

This prevents the program from crashing if someone types letters.

Combining Input and Output in Real Situations

Python I/O is used in everyday tasks like:

  • Creating sign-up forms

  • Running small games

  • Building calculators

  • Reading commands in automation scripts

  • Asking for file names before processing them

  • Taking menu choices in console applications

  • Displaying formatted reports or results

Even advanced applications depend on these basics.

Working with File I/O

Although this chapter focuses on console input and output, it’s helpful to know that Python can also read and write files.

Basic example:

file = open("data.txt", "w")
file.write("Hello from Python")
file.close()

And for reading:

file = open("data.txt", "r")
content = file.read()
print(content)
file.close()

You will learn this in detail in future chapters, but it connects naturally with I/O concepts.

Summary of the Tutorial

Python I/O lets your program communicate with the user. The input() function reads text, while the print() function displays results. Since all input arrives as a string, you often need to cast values before using them in calculations. Output formatting using f-strings, sep, and end helps you create clear messages. Whether you're building small utilities, games, forms, or scripts, mastering input and output is essential for writing interactive Python programs.


Practice Questions

  1. Why does the input() function always return a string, and how does this affect calculations?

  2. What is the difference between input and output in a Python program?

  3. How do the sep and end parameters change the behavior of the print() function?

  4. Why is input validation important when accepting values from the user?

  5. What advantages do f-strings offer when formatting output?

  6. Take a number from the user, cast it to an integer, and print its square.

  7. Write a program that asks for a first name and last name, then prints them together using a single print statement.

  8. Create a print statement that displays values on the same line using the end parameter.

  9. Format a price value to show only two decimal places using an f-string.

  10. Ask the user for two numbers in one input line using split(), convert them to integers, and print their sum.


Try a Short Quiz.

coding learning websites codepractice

No quizzes available.

Go Back Top