When you’re writing code in Python, one of the most common tasks you’ll come across is inserting variables into strings. That’s where formatted strings in Python come in. This feature makes your code more readable, efficient, and cleaner — especially when working with dynamic data.
In this article, we’ll answer the question: what is formatted string in Python, and explore different ways to format strings, including f-strings, .format()
method, and %
formatting. We’ll also address popular queries like What is the format of string?, What is the format of the F string?, and What is %s formatting in Python?
🔍 What is Formatted String in Python?
Formatted strings in Python are strings that allow embedding expressions or variables directly within the string. Instead of concatenating strings and variables manually, Python provides clean and powerful syntax for formatting strings.
Example:
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
Output:
My name is Alice and I am 25 years old.
This is called an f-string (available from Python 3.6 onwards).
❓ People Also Ask
🔹 What is the format of string?
In Python, the format of a string refers to the structure or template that defines how values should be embedded within a string using .format()
or f-strings.
🔹 What is the format of the F string?
The f-string is a type of formatted string introduced in Python 3.6. It starts with an f
or F
before the string and uses curly braces {}
to embed variables.
Example:
price = 10.5
print(f"The price is ${price}")
🔹 What is %s formatting in Python?
%s
is part of the old-style string formatting (also called printf-style). It is used to insert strings into placeholders marked with %s
.
Example:
name = "John"
print("Hello, %s" % name)
🧪 Types of Formatted Strings in Python
There are three main ways to create formatted strings in Python:
1️⃣ Old-Style Formatting (%
Operator)
This method is similar to how formatting works in C.
name = "David"
age = 30
print("My name is %s and I am %d years old." % (name, age))
%s
for string%d
for integer%f
for floating point
While it’s still supported, this style is now considered outdated.
2️⃣ str.format()
Method
This method was introduced in Python 2.6 and is widely used.
name = "Sara"
age = 28
print("My name is {} and I am {} years old.".format(name, age))
You can also use positional and named arguments:
print("My name is {0} and I am {1} years old.".format("Mike", 22))
print("My name is {name} and I am {age} years old.".format(name="Emma", age=29))
This method is flexible and supports advanced formatting like padding, decimal places, and alignment.
3️⃣ f-Strings (Formatted String Literals)
Introduced in Python 3.6, f-strings are the most modern and recommended way to format strings.
name = "Alex"
score = 92.678
print(f"Student {name} scored {score:.2f} in the test.")
You can even embed expressions:
print(f"The square of 7 is {7**2}")
📊 Comparison Table
Method | Syntax | Python Version | Recommended |
---|---|---|---|
% Formatting | "Hello %s" % name | 2.x and 3.x | ❌ No |
.format() | "Hello {}".format(name) | 2.6+ | ✅ Yes |
f-Strings | f"Hello {name}" | 3.6+ | ✅✅ Highly |
✨ Real-World Applications of Formatted Strings
- Displaying user data:
user = "Amit"
balance = 325.75
print(f"User: {user}, Balance: ${balance}")
- Generating reports:
report = f"""
Name : {name}
Age : {age}
Score : {score:.1f}
"""
print(report)
- Formatting logs or messages:
import datetime
now = datetime.datetime.now()
print(f"[{now}] - Application started")
🧠 Pro Tips
- Use f-strings for performance and readability.
- Format floats to specific decimal places:
{value:.2f}
- Use
str.format()
when using template strings or when supporting older Python versions. - Avoid
%
formatting in new code.
🧵 Conclusion
So, what is formatted string in Python? Simply put, it’s a powerful and elegant way to embed values and expressions inside strings. With f-strings, .format()
method, and %
formatting, Python gives you multiple options for building dynamic text output.
Whether you’re logging messages, creating reports, or building user interfaces, mastering formatted strings will take your Python skills to the next level. If you’re using Python 3.6 or later, f-strings are the way to go!