Input, Output & Comments
✕1. Output using print()
print()is a built-in function used to display output on the screen. We can useprint()to display: 📝 Text 🔢 Numbers 📦 Variables 🧮 Expressions Examples:print("Hello")# ➜ Helloprint(5 + 2)# ➜ 7name = "Rabindra"print(name)# ➜ Rabindraage = 30print("I am", age, "years old")# ➜ I am 30 years old- Text must be written inside quotes.
Example:
print("Hello, World!")Output: Hello, World! - Numbers and expressions can be printed directly.
Example:
print(25)print(5 + 2)print(10 * 3)Output: 25 7 30 - We can store a value in a variable and print it later.
Example:
name = "Rabindra"age = 30print(name)print(age)Output: Rabindra 30 - We can print multiple values by separating them with commas.
Example:
name = "Rabindra"age = 30print("My name is", name)print("I am", age, "years old")Output: My name is Rabindra I am 30 years old 📌 When we use commas insideprint(), Python automatically adds spaces between values.
1.1 What is print()?
1.2 Printing Text
1.3 Printing Numbers and Expressions
1.4 Printing Variables
1.5 Printing Multiple Values
2. f-strings
- Imagine a form letter with blanks:
Dear ___, your order of ___ will arrive on ___.
If we have values stored in variables, f-strings help us fill those blanks automatically.
Example
name = "Ram"item = "book"date = "Monday"print(f"Dear {name}, your order of {item} will arrive on {date}.")Output: Dear Ram, your order of book will arrive on Monday. f-strings are used to create formatted output by placing variables or expressions directly inside a string. Syntax:f"Text {variable}"📌 We must writefbefore the string. 📌 Variables or expressions are written inside curly braces{ }. - Without f-string:
name = "Rabindra"age = 30print("My name is", name, "and I am", age, "years old.")Output: My name is Rabindra and I am 30 years old. Using f-string:print(f"My name is {name} and I am {age} years old.")Output: My name is Rabindra and I am 30 years old. ✅ f-strings make the output cleaner and easier to understand. - We can also write expressions inside curly braces.
Example:
age = 30print(f"Next year, I will be {age + 1} years old.")Output: Next year, I will be 31 years old.
2.1 What are f-strings?
2.2 Why use f-strings?
2.3 Using Expressions inside f-strings
3. Format Specifiers
- A format specifier is a short instruction that tells Python exactly how we want a value to look. It is similar to choosing formatting options in Word, such as alignment, spacing, or decimal places.
Format specifiers are used inside f-strings to customize output. They help us control:
📏 Width
🎯 Decimal places
↔️ Alignment
✨ Padding characters
Syntax:
f"{variable:format_specifier}" - We can control the number of digits after decimal point.
Example:
num = 3.14159print(f"{num:.2f}")Output: 3.14 Here,.2fmeans display the number with 2 digits after decimal point. - Width means the minimum space given to display a value.
Example:
num = 123print(f"{num:10}")Output: 123 Here, width 10 means Python uses 10 spaces to display the value. - We can align values using:
<➜ Left align>➜ Right align^➜ Center align Example:item = "Apple"print(f"{item:<10}")# Left alignedprint(f"{item:>10}")# Right alignedprint(f"{item:^10}")# Center aligned - We can fill empty spaces with a character.
Example:
num = 123print(f"{num:->10}")# ➜ -------123print(f"{num:-<10}")# ➜ 123-------print(f"{num:#^11}")# ➜ ####123#### - Example:
print(f"{num:->10}")Here:-➜ Fill character>➜ Right alignment10➜ Width Example:print(f"{num:.2f}")Here:.2➜ Two digits after decimalf➜ Floating-point number item_1 = "Apple"price_1 = 3item_2 = "Banana"price_2 = 10item_3 = "Orange"price_3 = 200print(f"{'Item':<12}{'Price':>5}")print("-" * 17)print(f"{item_1:<12}{price_1:>5}")print(f"{item_2:<12}{price_2:>5}")print(f"{item_3:<12}{price_3:>5}")Output: Item Price ----------------- Apple 3 Banana 10 Orange 200
3.1 What are Format Specifiers?
3.2 🎯 Decimal Formatting
3.3 📏 Width Formatting
3.4 ↔️ Alignment
3.5 ✨ Fill Character
3.6 Format Specifier Breakdown
3.7 Table-like Output Example
3.8 Format Specifier Summary
| Specifier | Description |
|---|---|
| Fill | Character used for padding the output (default is space). Has to be single character |
| Align | Alignment of the output: < for left, > for right, ^ for center. Default: > for numbers and < for strings |
| Width | Minimum width of the output |
| Precision | Number of digits after decimal point for floating-point numbers |
Format Specifier Summary Table
4. User Input
input()is used to take input from the user. Example:name = input("Enter your name: ")print(f"Hello, {name}!")If user entersRabindra, output will be: Hello, Rabindra!input()always returns data as a string. Example:num_1 = input("Enter first number: ")num_2 = input("Enter second number: ")print(num_1 + num_2)If user enters: 10 5 Output: 105 😕 Why did Python show 105 instead of 15? This happened becauseinput()returns values as strings. So Python joined "10" and "5" as text.- To perform mathematical operations, we need to convert input into number.
Example:
num_1 = int(input("Enter first number: "))num_2 = int(input("Enter second number: "))total = num_1 + num_2print(f"Sum = {total}")If user enters: 10 5 Output: Sum = 15 - If the input can contain decimal value, use
float(). Example:price = float(input("Enter price: "))print(f"Price = Rs. {price:.2f}") length_in_cm = float(input("Enter length in cm: "))length_in_m = length_in_cm / 100print(f"Length = {length_in_m:.2f} m")
4.1 ⌨️ What is input()?
4.2 ⌨️ input() Always Returns String
4.3 ⌨️ Type Conversion with input()
4.4 ⌨️ Taking Float Input
4.5 ⌨️ Input and Calculation Example
5. Comments
- Comments are notes written inside code. They are not executed by Python. Comments are used to: 📝 Explain code 📌 Improve readability 🧠 Help us remember logic
- Single-line comments start with
#. Example:# This program displays a greeting messagename = "Rabindra"print(f"Hello, {name}!") - Comments can also be written beside code.
Example:
age = 30# stores age of userprint(age) - Python does not have a separate multi-line comment symbol like some other languages. However, triple quotes can be used to write multi-line notes when they are not assigned to a variable.
Example:
"""This program takes user informationand displays it in a formatted way."""name = "Rabindra"print(name)📌 Triple quotes actually create multi-line strings, but beginners often use them as block notes. - In VS Code, we can use:
Ctrl + /to comment or uncomment selected lines.
5.1 What are Comments?
5.2 Single-line Comments
5.3 Inline Comments
5.4 Block Notes using Triple Quotes
5.5 Comment Shortcut in VS Code
Practice QuestionsNot started
1. Output and f-strings
Question 1 of 5
- Create a variable
nameand display:Hello, {name}! - Create variables
nameandage. Display output in this format:My name is Python and I am 30 years old. - Create variables
itemandprice. Display output in this format:Ram bought a pen for Rs. 10.56. - Create variable
age. Display what the age will be next year using an f-string.
1.1 Practice output and f-string formatting:- Create a variable
2. Format Specifiers
Question 2 of 5
- Create a variable
num = 3.14159and display it up to 2 decimal places. - Create a variable
price = 125.5and display it in this format:Price: Rs. 125.50 - Create a variable
num = 123and display it with width 10. - Create a variable
num = 123and display it in these formats:-------123123-------####123#### - Create
itemandpricevariables and display them in table-like format:Item Price ----------------- Apple 3 Banana 10 Orange 200
2.1 Practice format specifier tasks:- Create a variable
3. User Input
Question 3 of 5
- Ask user for his/her name and display:
Hello, <name>! - Ask user for birth year and current year. Calculate and display his/her age.
- Ask user for length in feet. Convert and display its value in meter. Formula:
1 ft = 0.3048 m - Ask user for two numbers and display their: Sum, Difference, Product, Quotient. Note: Output should be in proper human-readable format, not just numbers.
- Ask user for length in cm. Convert it into meter and display the result up to 2 decimal places.
3.1 Practice tasks using input():- Ask user for his/her name and display:
4. Input, Calculation, and Formatting
Question 4 of 5
- Ask user for name and price of a pen. Display output in this format:
Ram bought a pen for Rs. 10.56. - Ask user for current year, month, and day. Display it in this format:
Today's Date: 2026-03-12 - Ask user for name and marks in 3 subjects. Calculate average percentage and display:
Hari scored 85.5% in exam. - Ask user for mass and length of a cube. Calculate and display its density.
Formula:
density = mass / volume,volume = length ** 3 - Ask user for a file name in format
file.ext. Extract and display the file extension.
4.1 Practice combined input, calculation, and formatting tasks:- Ask user for name and price of a pen. Display output in this format:
5. Comments
Question 5 of 5
- Write a program that displays your name and age. Add a comment explaining what the program does.
- Write a program that calculates area of a rectangle. Add comments explaining length, breadth, and area.
- Write a program that converts feet to meter. Add comments explaining the formula.
- Write a program with at least three lines of code and use
Ctrl + /in VS Code to comment and uncomment them.
5.1 Practice using comments in code:
