π’ LEVEL 1: EASY — What is an f-string?
An f-string (formatted string literal) allows you to insert variables directly inside a string using {}.
✅ Basic syntax
Output:
π’ Why use f-strings?
-
Easier than
+concatenation -
Faster than
format() -
Looks cleaner
π‘ LEVEL 2: MEDIUM — Using Expressions in f-strings
You can insert calculations or functions inside { }.
Example:
π‘ Formatting numbers
✔ Decimal formatting
✔ Add commas
✔ Percentage
π΅ LEVEL 3: HARD — Formatting Alignment & Padding
⭐ Left, Center, Right Alignment
⭐ Padding with characters
π΅ Using Dictionaries in f-strings
π΅ Nested f-strings (tricky but powerful)
Correct way:
π£ LEVEL 4: ADVANCED — Using f-strings for debugging
Python 3.8+ allows:
Output:
This is extremely useful for debugging.
π£ Embedding conditional (if) in f-strings
π£ Format dates using f-strings
Output:
π΄ LEVEL 5: EXTREME — Using f-strings with classes
π΄ Custom formatting using __format__
Output:
π SUMMARY
| Level | What You Learned |
|---|---|
| Easy | Basic f-string usage |
| Medium | Expressions, functions, number formatting |
| Hard | Alignment, padding, dictionary access |
| Advanced | Debugging, conditions, date formatting |
| Expert | Classes + custom formatting |
No comments:
Post a Comment