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"Hello {name}" � sabse clean aur modern tareeka. Python 3.6+ mein available.
"Hello {}".format(name) � purana tareeka. Ab bhi codebases mein milta hai.
"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.
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.
# 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.
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.50HOW: padding aur alignment
Tables aur aligned output ke liye padding use karo. < left align, > right align, ^ center align. Number ke baad width specify karo.
Quick comparison
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 22Quick 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
f"Hello name"� curly braces nahi lagaye toh variable substitute nahi hoga, literal "name" print hoga.f"Hello {name"� closing brace missing hai, SyntaxError aayega.- Quotes ke beech apostrophe use karna ho toh outer quotes ka type change karo:
f"It's {name}". .format()mein arguments count match karna zaroori hai � extra ya kam arguments se error aata hai.
Ab repeated code ko ek jagah bundle karna seekho � Python functions ke saath.