Input and Output in Python

In every programming language, input and output are fundamental operations.

Python provides simple and flexible ways to interact with users โ€” you can take input from the keyboard using the input() function and display output using the print() function.

This article explains how input() and print() work, along with different methods of string formatting โ€” such as f-strings, format(), and % formatting.

Input in Python โ€” input() Function

The input() function allows users to enter data from the keyboard during program execution. It always returns the input as a string (text), even if you type a number.

Example:

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

Enter your name: Julie
Hello, Julie

Type Casting with input()

By default, input() returns a string. If you want numeric input, you must convert (cast) it using int() or float().

Example:

num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
print("Sum:", num1 + num2)
Output:

Enter first number: 5
Enter second number: 10
Sum: 15

Output in Python โ€” print() Function

The print() function is used to display output on the screen. It can print strings, numbers, variables, and even multiple values separated by commas.

Syntax:

print(object1, object2, ..., sep=' ', end='\n')
Parameter Description
sep Defines the separator between values (default: space ' ')
end Defines what to print at the end (default: newline '\n')
Example:

name = "Python"
version = 3
print("Welcome to", name, "version", version)
Output:

Welcome to Python version 3
Using 'sep' and 'end':

print("A", "B", "C", sep="-")   # A-B-C
print("Hello", end=" ")         
print("World")                  # Hello World

String Formatting in Python

When printing variables or messages, you often need to mix text with dynamic values. Python provides multiple ways to format strings neatly and clearly.

f-Strings (Python 3.6+)

f-strings (formatted string literals) are the most modern and preferred method. They allow you to embed expressions inside curly braces {} directly in a string by prefixing it with the letter f.

Example:

name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:

My name is Alice and I am 25 years old.
You can even perform calculations inside {}:

Example:

a, b = 5, 10
print(f"The sum of {a} and {b} is {a + b}")
Output:

The sum of 5 and 10 is 15

Using format() Method

The str.format() method is an older but still widely used approach. You can insert placeholders {} into a string, which are replaced by values passed to the format() function.

Example:

name = "Bob"
age = 30
print("My name is {} and I am {} years old.".format(name, age))
Output:

My name is Bob and I am 30 years old.
You can also use positional and keyword arguments:

Example:

print("Hello {0}, you have {1} new messages.".format("Alice", 5))
print("Coordinates: Latitude={lat}, Longitude={lon}".format(lat=27.2, lon=78.0))
Output:

Hello Alice, you have 5 new messages.
Coordinates: Latitude=27.2, Longitude=78.0

Old-Style % Formatting

This is the oldest way of string formatting (from C language style). It uses % as a placeholder for variable values.

Example:

name = "Charlie"
age = 22
print("My name is %s and I am %d years old." % (name, age))
Output:

My name is Charlie and I am 22 years old.
Symbol Meaning
%s String
%d Integer
%f Float (decimal number)
Example (float formatting):

pi = 3.14159
print("Value of pi = %.2f" % pi)
Output:

Value of pi = 3.14

Quick Comparison of Formatting Methods

Method Introduced In Example Modern Use
% formatting Legacy "Hello %s" % name โŒ Outdated
format() Python 2.6+ "Hello {}".format(name) โš™๏ธ Common
f-strings Python 3.6+ f"Hello {name}" โœ… Recommended

Summary

In this lesson, you learned how to take user input, display output, and format strings using different techniques. These concepts are essential for interactive programs and clean console outputs.

Concept Function/Feature Description
input() Takes input from the user (always as string)
print() Displays output on the screen
f-strings Modern and easiest way for string formatting
format() Flexible and powerful formatting method
% formatting Older C-style formatting
In the next article, we'll cover Control Statements in Python โ€” learning how to use if, elif, else, and nested conditions to control the flow of your program.
Share this Article