Python Tutorial - String Formatting

Unlock Premium Features: AI explanations (Hinglish), Indian voice & Videos
Sign Up Free

๐ŸŽจ String Formatting - Pretty Text Banao

๐ŸŒŸ Real Life Mein:

Messages format karna:

๐Ÿ’ณ "Dear Rohan, Your bill is โ‚น500"
๐Ÿ“ง "Hello Simran, Your order #123 is shipped"
๐ŸŽซ "Ticket: Row 5, Seat 12, Price โ‚น300"

Variables ko text mein daalna = String Formatting!

๐Ÿ“ Formatting Methods

Method 1: Concatenation (+)
name = "Rohan"
age = 15

# Old way - difficult
message = "Hello " + name + ", you are " + str(age) + " years old"
print(message)
Method 2: f-strings (Modern - Best!)
name = "Rohan"
age = 15

# f-string - Easy aur clean!
message = f"Hello {name}, you are {age} years old"
print(message)

# Math bhi kar sakte ho
price = 100
discount = 20
print(f"Price: โ‚น{price - discount}")

๐Ÿ’ฐ Bill Generator Example

Restaurant Bill
customer = "Amit"
item1 = "Pizza"
item2 = "Coke"
price1 = 200
price2 = 50

# Bill format
print("=" * 30)
print(f"Customer: {customer}")
print("=" * 30)
print(f"{item1:<15} โ‚น{price1:>6}")
print(f"{item2:<15} โ‚น{price2:>6}")
print("-" * 30)
total = price1 + price2
print(f"Total{"":<10} โ‚น{total:>6}")
print("=" * 30)

๐ŸŽฏ Formatting Options:

  • {name:10} - 10 characters wide
  • {price:.2f} - 2 decimal places
  • {num:05d} - 5 digits, leading zeros

๐Ÿค– AI Tutor Unlock Karo!

Apni language mein coding seekho - Hindi, Marathi, Gujarati aur 10+ Indian languages mein!

  • Hinglish mein explanations
  • Real-life examples
  • Beginner-friendly
Free Signup Karo

Example

name = "Python"
version = 3.12
print(f"I love {name} {version}")