π§© 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