Lesson 20 of 30 � 16 minBeginner

String Formatting (f-strings)

Strings ko format karna output dikhane ke liye zaroori hai � prices, dates, names sab ko properly present karna padta hai.

Prerequisite: User Input

Kyun zaroori hai string formatting?

Jab aap output print karte ho � chahe receipt ho, report card ho, ya simple message � usmein variables ko clean tareeke se dikhana padta hai. Bina formatting ke code messy aur unreadable ho jaata hai. String formatting se aap variable values ko directly string ke andar inject kar sakte ho.

F-STRINGS

f"Hello {name}" � sabse clean aur modern tareeka. Python 3.6+ mein available.

.format()

"Hello {}".format(name) � purana tareeka. Ab bhi codebases mein milta hai.

%-FORMATTING

"Hello %s" % name � sabse purana C-style tareeka. Avoid karo.

HOW: f-strings se start karo

f-string mein string ke before f lagao, aur variable ya expression ko curly braces {} ke andar likho. Braces ke andar kuch bhi likh sakte ho � variable, calculation, method call.

python
name = "Aman"
age = 22
print(f"Hello {name}, you are {age} years old!")
# Hello Aman, you are 22 years old!

print(f"10 * 5 = {10 * 5}")
# 10 * 5 = 50

price = 999.5
print(f"Price: ?{price:.2f}")
# Price: ?999.50

print(f"Discount: {20}%")
# Discount: 20%

.2f ka matlab hai 2 decimal places tak format karo. Yeh prices, percentages aur numerical data dikhane mein bahut kaam aata hai.

.format() method � purana lekin useful

f-string se pehle .format() standard tha. Curly braces mein position ya keyword daal ke values inject hoti hain.

python
# Positional arguments
print("Hello {} from {}".format("World", "Lucknow"))
# Hello World from Lucknow

# Named arguments
print("Hello {name}, city is {city}".format(name="Riya", city="Delhi"))
# Hello Riya, city is Delhi

# Reuse same value
print("{0} is {0}".format("Python"))
# Python is Python

%-formatting � legacy style

Sabse purana tareeka hai C-style formatting. %s strings ke liye, %d integers ke liye, %f floats ke liye. Naye code mein avoid karo, lekin purane codebase samajhne ke liye jaanna zaroori hai.

python
name = "Aman"
age = 22
print("Hello %s, you are %d years old!" % (name, age))
# Hello Aman, you are 22 years old!

price = 999.5
print("Price: ?%.2f" % price)
# Price: ?999.50

HOW: padding aur alignment

Tables aur aligned output ke liye padding use karo. < left align, > right align, ^ center align. Number ke baad width specify karo.

Try it: report card with f-stringsCode edit karo aur output dekho
Run Python dabayein

Quick comparison

python
name = "Aman"
age = 22

# f-string (recommended)
print(f"Hello {name}, age {age}")

# .format()
print("Hello {}, age {}".format(name, age))

# %-formatting (legacy)
print("Hello %s, age %d" % (name, age))

# All three print: Hello Aman, age 22

Quick check

name variable ki value print karne wali f-string likho.

String ke before f lagao aur variable ko curly braces ke andar likho.

Common mistakes

Ab aap clean output likh sakte ho!

Ab repeated code ko ek jagah bundle karna seekho � Python functions ke saath.