Input and Output in Python

Input and Output are essential parts of every Python program because they allow interaction between the program and the user. Input refers to receiving data from the user, while output refers to displaying information back to the screen. Python provides simple and powerful tools to handle both operations. Learning how to take input and display formatted output is the foundation for building interactive programs such as calculators, forms, data entry systems, and more.

1. Taking User Input

Python uses the built-in input() function to read data entered by the user. The function always returns the input as a string, even if the user types a number. This means the program must often convert the input to the appropriate type before using it. Taking input allows programs to become dynamic and flexible instead of using fixed values in the code.

Basic Input Example

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

Output:

Enter your name: Rahul
Hello, Rahul

This program waits for the user to type something and then prints it back.

2. Converting Input Types

Since input() always returns a string, it must be converted when working with numbers. Python provides type conversion functions such as int() for integers and float() for decimal numbers.

Integer and Float Input Example

age = int(input("Enter your age: "))
height = float(input("Enter your height: "))
print("Age:", age)
print("Height:", height)

Output:

Enter your age: 20
Enter your height: 5.7
Age: 20
Height: 5.7

Type conversion ensures the values can be used in calculations or comparisons.

3. Taking Multiple Inputs

Sometimes the user needs to enter several values in one line. Python supports this by using the split() function or map() for type conversion. This is particularly useful when processing sequences of data such as lists, mathematical expressions, or configuration values.

Multiple Input Example

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

Output:

Enter two numbers: 10 20
10 20

4. Output Formatting

Output formatting refers to controlling how information appears when printed. Instead of simple print statements, Python provides several methods to format text, align values, apply styles, and embed variables inside strings. Proper formatting improves readability and makes output look structured and professional.

Python offers four major formatting techniques:

  • f-strings
  • str.format()
  • Manual formatting
  • Old-style % formatting

4.1 Formatted String Literals (f-strings)

f-strings allow inserting variables directly inside curly braces {}. They are fast, easy to read, and support advanced formatting options such as alignment and decimal precision.

Basic f-string Example

name = "Alice"
print(f"Hello, {name}")

Output:

Hello, Alice

Formatting Numbers

import math
print(f"Pi value is {math.pi:.3f}")

Output:

Pi value is 3.142

The value is rounded to three decimal places.

Alignment with f-strings

for i in range(1, 4):
   print(f"{i:3} | {i*i:4}")

Output:

 1 |    1
 2 |    4
 3 |    9

Here each number is placed inside a fixed width.

4.2 The str.format() Method

Before f-strings were introduced, the str.format() method was widely used. It still remains useful due to its flexibility and compatibility with older Python versions.

Basic format Example

print("Hello, {}".format("Alice"))

Output:

Hello, Alice

Positional Arguments

print("{1} and {0}".format("spam", "eggs"))

Output:

eggs and spam

Keyword Arguments

print("This {food} is {taste}".format(food="pizza", taste="delicious"))

Output:

This pizza is delicious

Table Formatting

for i in range(1, 6):
   print("{0:2d} {1:3d} {2:4d}".format(i, i*i, i*i*i))

Output:

1   1    1
2   4    8
3   9   27
4  16   64
5  25  125

4.3 Manual String Formatting

Python provides several built-in string methods that allow aligning or padding text manually. These functions do not rely on placeholders and give direct control over the appearance of the output.

Alignment Methods

print("Hi".rjust(5))
print("Hi".ljust(5))
print("Hi".center(10))

Output:

    Hi
Hi   
  Hi    

Zero-Padding using zfill()

print("42".zfill(5))
print("-3.14".zfill(7))

Output:

00042
-003.14

4.4 Old-Style % Formatting

This formatting style comes from the C language and was commonly used in early Python versions. It is less preferred today but still appears in older code bases.

Example

name = "Alice"
print("Hello, %s" % name)

Output:

Hello, Alice

Number Formatting

num = 3.14159
print("Pi is %.2f" % num)

Output:

Pi is 3.14

4.5 str() vs repr()

Both str() and repr() convert values to string form, but they serve different purposes. str() is meant for readable output for users, while repr() is meant for debugging and shows the exact representation of the object, including escape characters.

Example

text = "Hello\nWorld"
print(str(text))
print(repr(text))

Output:

Hello
World
‘Hello\nWorld’

4.6 Template Strings

Template strings provide another method for formatting, especially useful when working with external user data or generating templates for emails or messages. They are defined in the string module.

Example

from string import Template
t = Template("Hello $name")
print(t.substitute(name="Alice"))

Output:

Hello Alice