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.
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.
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.
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.
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.
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.
a, b = input("Enter two numbers: ").split()
print(a, b)
Output:
Enter two numbers: 10 20
10 20
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 allow inserting variables directly inside curly braces {}. They are fast, easy to read, and support advanced formatting options such as alignment and decimal precision.
name = "Alice"
print(f"Hello, {name}")
Output:
Hello, Alice
import math
print(f"Pi value is {math.pi:.3f}")
Output:
Pi value is 3.142
The value is rounded to three decimal places.
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.
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.
print("Hello, {}".format("Alice"))
Output:
Hello, Alice
print("{1} and {0}".format("spam", "eggs"))
Output:
eggs and spam
print("This {food} is {taste}".format(food="pizza", taste="delicious"))
Output:
This pizza is delicious
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
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.
print("Hi".rjust(5))
print("Hi".ljust(5))
print("Hi".center(10))
Output:
Hi
Hi
Hi
print("42".zfill(5))
print("-3.14".zfill(7))
Output:
00042
-003.14
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.
name = "Alice"
print("Hello, %s" % name)
Output:
Hello, Alice
num = 3.14159
print("Pi is %.2f" % num)
Output:
Pi is 3.14
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.
text = "Hello\nWorld"
print(str(text))
print(repr(text))
Output:
Hello
World
‘Hello\nWorld’
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.
from string import Template
t = Template("Hello $name")
print(t.substitute(name="Alice"))
Output:
Hello Alice