🟢 Beginner · Lesson 06
Input and Output in Python
Input and Output
Programs talk to the user in two ways: output (showing results with print()) and input (reading data with input()). Together they make a program interactive.
Output with print()
You already know print(). It can print text, variables, and several items separated by commas.
Python – out.py
name = "Aman"
print("Hello", name, "welcome!")Hello Aman welcome!
Input with input()
input() pauses the program and waits for the user to type. It always returns a string — convert it with int() or float() for numbers.
⚠️ Remember
input() gives text. "5" + "3" becomes "53", not 8. Always convert before doing maths.
Program 1: Greet the User
Python – greet.py
name = input("What is your name? ")
print("Nice to meet you,", name)What is your name? Riya
Nice to meet you, Riya
input("What is your name? ") shows the prompt and stores the typed text in name.
Program 2: Sum of Two Numbers
Python – sum.py
a = int(input("First number : "))
b = int(input("Second number: "))
total = a + b
print("Sum =", total)First number : 12
Second number: 8
Sum = 20
int(input(...))reads text and converts to integer in one step.- Without
int(),a + bwould join the strings instead of adding.
Program 3: Simple Interest
Python – interest.py
p = float(input("Principal: "))
r = float(input("Rate %: "))
t = float(input("Time (years): "))
si = (p * r * t) / 100
print("Simple Interest =", si)Principal: 1000
Rate %: 5
Time (years): 2
Simple Interest = 100.0
We use float() here because rate and time can have decimals.
f-strings (Modern Formatting)
The cleanest way to mix text and variables is an f-string — put f before the quote and variables inside { }.
Python – fstring.py
name = "Aman"
marks = 95
print(f"{name} scored {marks} marks")Aman scored 95 marks
Common Mistakes
- Forgetting to convert
input()withint()/float(). - Adding two inputs and getting joined text instead of a sum.
- Missing the space at the end of the prompt text.
Practice Tasks
- Ask the user's age and print the year they were born.
- Read three subject marks and print the total and average.
- Ask for a temperature in Celsius and print it in Fahrenheit.
Summary
print()shows output;input()reads input.input()always returns a string — convert for maths.- f-strings (
f"{var}") are the cleanest way to format output.
💻 Live Code Editor
This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.