🧩 Python String Formatting – Insert Dynamic Data into Strings | TechTown.in
Want to insert values into strings in a clean, readable, and professional way?
That’s where Python String Formatting comes in — giving you full control over how text and variables are combined.
In this post, we’ll break down all the ways to format strings in Python: using format(), f-strings, and advanced formatting options with real-life examples.
🎯 Why Use String Formatting?
Sometimes you need to display variables within strings, like this:
name = "Tanmay"
print("Hello, " + name)
But as your code gets more complex, simple concatenation becomes messy.
✅ String formatting makes your output:
- Clean
- Flexible
- Professional
🛠️ The .format() Method
name = "Tanmay"
age = 23
print("My name is {} and I am {} years old.".format(name, age))
🎯 Output:
My name is Tanmay and I am 23 years old.
📌 Positional & Keyword Formatting
🔹 Positional:
print("I like {} and {}".format("Python", "JavaScript"))
🔹 With Index:
print("I like {1} and {0}".format("Python", "JavaScript"))
🎯 Output: I like JavaScript and Python
🔹 Keyword:
print("My name is {name} and I'm from {city}".format(name="Tanmay", city="Jaipur"))
⚡ Python f-Strings (Python 3.6+)
The cleanest way to format strings.
name = "Tanmay"
age = 23
print(f"My name is {name} and I am {age} years old.")
✅ Faster and more readable than .format().
🔢 Format Numbers with format()
Fixed decimal points:
price = 49.987
print("Price: {:.2f}".format(price)) # Output: Price: 49.99
Thousands separator:
num = 1000000
print("Views: {:,}".format(num)) # Output: Views: 1,000,000
📏 Align Text in Output
txt = "|{:<10}|{:^10}|{:>10}|"
print(txt.format("Left", "Center", "Right"))
🎯 Output:
|Left | Center | Right|
📐 Width, Padding & Alignment
| Format | Example |
|---|---|
| Left-align | "{:<10}".format("Python") |
| Right-align | "{:>10}".format("Python") |
| Center-align | "{:^10}".format("Python") |
| Pad with zeros | "{:05d}".format(42) |
| Float with 2 decimals | "{:.2f}".format(3.14159) |
🔁 Combine f-Strings with Expressions
a = 10
b = 20
print(f"Sum of {a} and {b} is {a + b}")
🎯 Output: Sum of 10 and 20 is 30
🧪 Real-Life Use Case – Invoice Generator
item = "Laptop"
price = 49999.99
print(f"Product: {item}\nPrice: ₹{price:,.2f}")
🎯 Output:
Product: Laptop
Price: ₹49,999.99
📝 Python String Formatting Cheat Sheet
| Task | Code Example |
|---|---|
| Basic formatting | "Hello {}".format(name) |
| f-string | f"Hello {name}" |
| Decimal formatting | "${:.2f}".format(price) |
| Thousand separator | "{:,}".format(1000000) |
| Text alignment | "{:^20}".format("Centered") |
🧠 Best Practices
- ✅ Use f-strings for most modern Python projects (cleaner & faster)
- ✅ Use
.format()when working in legacy systems (Python 3.5 or earlier) - ❌ Avoid string concatenation for multi-variable outputs
- ✅ Use formatting to improve readability and presentation
🏁 Final Thoughts
Whether you’re writing scripts, APIs, or web apps — string formatting is essential for clean, dynamic, and readable output.
With just a little practice, you can make your Python outputs look as polished as any professional app.
📘 Explore more essential Python concepts at TechTown.in

