String formatting is an important concept in Python programming that helps developers create clean, readable, and dynamic output. It allows you to insert variables into strings and control how data is displayed.
Python provides multiple ways to format strings, including f-strings, the format() method, and older formatting techniques. Understanding these methods is essential for writing professional and efficient Python code.

What is String Formatting in Python?
String formatting means inserting variables or expressions inside a string in a structured way. Instead of manually combining strings and variables, Python provides built-in methods to simplify the process.
Example:
name = "Amit"
print("Hello", name)
But string formatting allows better control and readability.
1. F-Strings (Formatted String Literals)
F-strings are the most modern and fastest way to format strings in Python. They were introduced in Python 3.6.
You simply add an f before the string and use {} to insert variables.
Example:
name = "Rahul"
age = 20print(f"My name is {name} and I am {age} years old.")
Advantages of F-Strings:
- Easy to write and read
- Faster performance
- Supports expressions inside braces
Example with expression:
a = 10
b = 20
print(f"Sum is {a + b}")
2. format() Method
The format() method is another powerful way to format strings in Python. It uses curly braces {} as placeholders.
Example:
name = "Priya"
age = 22print("My name is {} and I am {} years old".format(name, age))
Indexed Format:
print("My name is {0} and age is {1}".format("Amit", 21))
Named Placeholders:
print("My name is {name} and age is {age}".format(name="Ravi", age=25))
3. Old % Formatting (Legacy Method)
Before f-strings and format(), Python used % formatting.
Example:
name = "Sita"
age = 23print("My name is %s and I am %d years old" % (name, age))
Although still supported, it is now less commonly used.
Comparison of Methods
| Method | Ease of Use | Performance | Recommendation |
|---|---|---|---|
| f-strings | Very Easy | Fastest | Best Choice |
| format() | Easy | Moderate | Good Option |
| % formatting | Old style | Slow | Avoid |
Why String Formatting is Important
String formatting is widely used in real-world applications like:
- Displaying user data
- Creating reports
- Web development outputs
- Data analysis results
- Logging and debugging
It helps make output more readable and professional.
Advanced Formatting Features
Python also allows advanced formatting such as:
- Controlling decimal places
- Aligning text
- Padding numbers
Example:
price = 99.5678
print(f"Price: {price:.2f}")
Output:
Price: 99.57
Python string formatting is a powerful feature that improves code readability and output control. Among all methods, f-strings are the most modern and efficient way to format strings.
By mastering f-strings, format() method, and other techniques, beginners can write cleaner and more professional Python programs.
For More Information and Updates, Connect With Us
- Name Sumit singh
- Phone Number: +91 9264477176
- Email ID: emancipationedutech@gmail.com
- Our Platforms:
- Digilearn Cloud
- Live Emancipation
- Follow Us on Social Media:
- Instagram – Emancipation
- Facebook – Emancipation
Stay connected and keep learning with Python Training !