Python offers modern string interpolation via f-strings (formatted string literals), introduced in Python 3.6 and expanded in 3.8+ (f"{var=}").
flowchart TD
A["Format Options"] --> B["Legacy % Operator (e.g. '%s %d' % (a, b))"]
A --> C["str.format() (e.g. '{} {}'.format(a, b))"]
A --> D["Modern F-Strings (e.g. f'{a} {b}')"]
D --> E["Supports Expressions, Debug specifiers, and Format Specs"]
f"{val:.2f}": Float rounded to 2 decimal places.f"{val:,}": Large integer with comma thousands separators (1,000,000).f"{val:>10}": Right-aligned string padded to width 10.f"{val=}": Debug mode printing variable name and value (val=42).from datetime import datetime
def format_financial_report(company: str, revenue: float, margin: float) -> str:
timestamp = datetime.now()
# Advanced F-String Format Specifiers
report = f"""
========================================
FINANCIAL STATEMENT: {company.upper():^20}
Timestamp: {timestamp:%Y-%m-%d %H:%M}
========================================
Gross Revenue : ${revenue:>15,.2f}
Profit Margin : {margin:>15.1%}
========================================
"""
return report
if __name__ == "__main__":
sample_report = format_financial_report("Kodersolution Corp", 1250450.80, 0.2345)
print(sample_report)
# Debug format specifier (Python 3.8+)
x = 42
y = 99
print(f"Debug Specifier Output: {x=} | {y=} | {x+y=}")
str.format() or %.f"{data['key']}" rather than double quotes everywhere).Write an f-string formatting a float 1234.5678 as currency $1,234.57 (with comma separators and 2 decimal places).
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With