Last modified: Feb 19, 2026 By Alexander Williams

Python Format Output Guide: Strings & Numbers

Creating clear output is a key programming skill. In Python, you have several powerful tools to format strings and numbers. This guide covers the main methods.

You will learn about f-strings, the format() method, and the older %-operator. Each method helps you present data neatly.

Why Format Output in Python?

Raw data is hard to read. Formatting makes it user-friendly. You can control decimal places, add commas to large numbers, and create clean reports.

Good formatting improves debugging and user experience. It turns messy data into clear information.

Method 1: F-Strings (Python 3.6+)

F-strings are the modern and preferred way. They are fast and readable. You prefix a string with 'f' and embed variables in curly braces {}.


# Basic f-string example
name = "Alice"
score = 95.5
print(f"Player {name} scored {score} points.")
    

Player Alice scored 95.5 points.

You can also perform operations inside the braces. This is very powerful for inline calculations.


# Expressions inside f-strings
price = 49.99
quantity = 3
print(f"Total cost: ${price * quantity:.2f}")
    

Total cost: $149.97

Method 2: The str.format() Method

The format() method is versatile and works in all Python 3 versions. You use placeholders {} in the string and pass values to the method.


# Basic format() method
template = "Welcome to {}, {}!"
city = "London"
year = 2024
print(template.format(city, year))
    

Welcome to London, 2024!

You can use numbered or named placeholders for more control. This prevents errors if the order of arguments changes.


# Named placeholders with format()
message = "Product: {item}, Price: ${cost:.2f}"
print(message.format(item="Laptop", cost=1299.99))
    

Product: Laptop, Price: $1299.99

Method 3: The Old %-Formatting

This is the oldest method, similar to C's printf. While not recommended for new code, you might see it in older projects. It uses % as a placeholder.


# %-formatting example
data = ("Python", 3.12)
print("Language: %s, Version: %.2f" % data)
    

Language: Python, Version: 3.12

Formatting Numbers and Decimals

All three methods allow number formatting. You can set decimal places, add thousand separators, and define padding.

This is crucial for financial data or scientific results. It ensures consistency in your output.


# Formatting numbers with f-strings
value = 1234567.8912
print(f"Default: {value}")
print(f"Two decimals: {value:.2f}")
print(f"With comma: {value:,.2f}")
print(f"Percent: {0.8765:.1%}")
    

Default: 1234567.8912
Two decimals: 1234567.89
With comma: 1,234,567.89
Percent: 87.7%

Aligning and Padding Text

You can align text within a specific width. Use < for left, > for right, and ^ for center alignment.

This is useful for creating tables or reports with neat columns.


# Text alignment and padding
items = [("Apple", 2.5), ("Banana", 1.3), ("Cherry", 4.0)]
for item, price in items:
    print(f"{item:<10} ${price:>6.2f}")
    

Apple      $  2.50
Banana     $  1.30
Cherry     $  4.00

Which Method Should You Use?

For new projects, always use f-strings. They are the cleanest and fastest. Use format() if you need compatibility with slightly older Python versions.

Avoid the %-operator for new code. It is less readable and more error-prone than the other options.

Mastering these techniques is a fundamental step in becoming proficient with Python's core data types and operations.

Conclusion

Python offers excellent tools for formatting output. F-strings provide a modern and efficient syntax. The format() method offers great flexibility for complex tasks.

Choose the right tool for your project and Python version. Clean formatting makes your code's output professional and easy to understand. Start practicing with f-strings today.