Format Strings
String formatting in Python is used to insert variables and expressions inside strings in a clean and readable way.
It helps create dynamic output messages without complex concatenation.
Python provides multiple methods for formatting strings, from basic to modern.
What Is String Formatting?
String formatting allows you to:
- Combine text and variables
- Display values inside strings
- Control output format
Definition
String formatting is the process of embedding values inside a string.
Format Strings Using f-Strings (Recommended)
f-strings are the modern and most preferred way to format strings in Python.
They were introduced in Python 3.6.
Syntax
python
f"string {variable}"
Code Example 1: Basic f-string
python
name = "Python"
print(f"Welcome to {name}")
Code Example 2: Multiple Variables
python
name = "John"
age = 25
print(f"Name: {name}, Age: {age}")
Code Example 3: Expression Inside f-string
python
a = 10
b = 20
print(f"Sum is {a + b}")
Best Practice
Always use f-strings for clean and readable formatting.
Format Strings Using format() Method
The
format() method is an older but still valid way to format strings.Code Example 1: Single Placeholder
python
name = "Python"
print("Welcome to {}".format(name))
Code Example 2: Multiple Placeholders
python
name = "John"
age = 30
print("Name: {}, Age: {}".format(name, age))
Code Example 3: Indexed Placeholders
python
print("{1} is learning {0}".format("Python", "John"))
Format Strings Using Named Placeholders
You can use named placeholders with the
format() method.python
print("Name: {name}, Age: {age}".format(name="Alice", age=28))
python
print("Course: {course}".format(course="AWS"))
python
print("{lang} is easy".format(lang="Python"))
Old Style Formatting (% Operator)
This is the oldest method of string formatting in Python.
It is still supported but not recommended for new code.
Code Example 1: String Formatting
python
name = "Python"
print("Welcome to %s" % name)
Code Example 2: Integer Formatting
python
age = 25
print("Age is %d" % age)
Code Example 3: Multiple Values
python
name = "John"
age = 30
print("Name: %s, Age: %d" % (name, age))
Note
Avoid % formatting in modern Python code.
Formatting Numbers in Strings
Python allows formatting numbers easily.
Code Example 1: Decimal Places
python
price = 49.5678
print(f"Price is {price:.2f}")
Code Example 2: Padding Numbers
python
num = 5
print(f"Value is {num:03}")
Code Example 3: Percentage Format
python
value = 0.85
print(f"Success rate: {value:.0%}")
Multiline Format Strings
f-strings work perfectly with multi-line strings.
python
name = "Python"
version = 3
print(f"""
Language: {name}
Version: {version}
Status: Active
""")
Common Mistakes
Mixing + Instead of Formatting
python
age = 20
print("Age is " + str(age))
Better approach:
python
print(f"Age is {age}")
Summary
- String formatting inserts values into strings
- f-strings are the best and modern approach
format()is an alternative method%formatting is outdated- Formatting improves readability and flexibility
Exercise
- Format your name and age using f-string
- Print sum of two numbers inside a string
- Format a float value to 2 decimal places