Learning how to use string formatters in Python 3 is one of the most essential and frequently used skills in modern Python programming — whether you’re printing clean console output, generating reports, creating logs, building CLI tools, sending formatted emails, or displaying data in web apps, using string formatters in Python 3 makes your text output readable, professional, and user-friendly.

In this up-to-date 2025–2026 guide, you’ll master exactly how to use string formatters in Python 3: f-strings (the modern standard), str.format() (positional & keyword), legacy % operator, type specifiers (d, f, s), padding/alignment, precision control, and best practices. All examples are tested on Python 3.10–3.13.

Key Takeaways – How to Use String Formatters in Python 3

  • Use string formatters in Python 3 with f-strings (f”…”) — cleanest, fastest, and most readable method (Python 3.6+).
  • str.format() supports positional {} and keyword {name} arguments.
  • Legacy % operator still works but is discouraged in new code.
  • Control precision: {:.2f} for 2 decimal places, {:,} for thousands separator.
  • Alignment & padding: {:<10}, {:>10}, {:^10}, {:0>5}.
  • Type specifiers: d (integer), f (float), s (string), e (scientific), % (percentage).
  • f-strings support expressions: f”Next year: {age + 1}” — powerful and Pythonic.

Prerequisites

  • Python 3.6+ installed (3.12 or 3.13 recommended)
  • Basic Python knowledge (print, variables, strings)
  • Interactive shell (python3) or script file

1. f-Strings – Best & Modern Way to Use String Formatters in Python 3

f-strings (formatted string literals) are the recommended way to use string formatters in Python 3 — introduced in 3.6, they are simple, fast, and readable:

				
					name = "Zain"
age = 30
city = "Karachi"
score = 95.7

print(f"Hello, {name}! You are {age} years old and live in {city}.")
# Hello, Zain! You are 30 years old and live in Karachi.

print(f"Next year you'll be {age + 1} | Score: {score:.1f}")
# Next year you'll be 31 | Score: 95.7
				
			

Powerful features:

  • Expressions inside {}: {age + 1}, {len(name)}
  • Formatting codes: {score:.2f} (2 decimals), {num:,} (thousands separator), {date:%Y-%m-%d}
  • Alignment & padding: {name:>20} (right), {name:<20} (left), {name:^20} (center), {num:05d} (zero-pad)

2. str.format() – Classic Positional & Keyword Formatting

Still widely used and very powerful:

Positional (index-based):

				
					print("Sammy has {} balloons today!".format(5))
# Sammy has 5 balloons today!

print("Sammy the {0} has a pet {1}!".format("shark", "pilot fish"))
# Sammy the shark has a pet pilot fish!

# Reorder with indices
print("Sammy the {1} has a pet {0}!".format("shark", "pilot fish"))
# Sammy the pilot fish has a pet shark!
				
			

Keyword arguments:

				
					print("Sammy the {pr} {verb} a {noun}.".format(
    pr="pull request", verb="made", noun="shark"
))
# Sammy the pull request made a shark.
				
			

3. Legacy % Operator (Still Valid, But Avoid in New Code)

				
					print("Sammy ate %d percent of a pizza!" % 75)
# Sammy ate 75 percent of a pizza!

print("Pi ≈ %.2f" % 3.14159)
# Pi ≈ 3.14
				
			

Recommendation: Use f-strings for all new code in 2025–2026 — cleaner, faster, less error-prone.

4. Advanced Formatting Controls

Precision & Type Specifiers:

				
					pi = 3.1415926535
print(f"Pi ≈ {pi:.4f}")          # Pi ≈ 3.1416
print("{:.0f}".format(75.765))    # 76 (rounds)
print("{:d}".format(42))          # 42 (integer)
print("{:e}".format(123456))      # 1.234560e+05 (scientific)
				
			

Padding & Alignment:

				
					print("{:>10}".format("right"))   #     right
print("{:<10}".format("left"))    # left      
print("{:^10}".format("center"))  #  center   
print("{:0>5}".format(42))        # 00042 (zero-pad)
print("{:*<20}".format("Sammy"))  # Sammy***************
				
			

Thousands Separator & Percentage:

				
					print(f"{1000000:,}")             # 1,000,000
print(f"{0.756:.1%}")             # 75.6%
				
			

5. Best Practices & Modern Tips (2025–2026)

  • Prefer f-strings for readability & performance.
  • Use expressions inside f-strings: f”Total: {price * quantity:.2f}”
  • Avoid % formatting in new code — deprecated in many style guides.
  • Escape curly braces in f-strings: f”Literal braces: {{ {value} }}”.
  • Combine with methods: f”Name: {name.upper():<20}”.
  • For very complex formatting → use str.format() or template libraries.
  • Always validate input before formatting — prevents errors.

How to Use String Formatters in Python 3 – FAQ (2025–2026)

  1. How do I use string formatters in Python 3?
    Best way: f-strings f”Hi {name}!” — cleanest way to use string formatters in Python 3.
  2. What’s the difference between f-strings and str.format() in Python 3?
    f-strings are shorter, faster, support expressions; .format() is older but still powerful.
  3. How do I align text when using string formatters in Python 3?
    {:<10} left, {:>10} right, {:^10} center — key when using string formatters in Python 3.
  4. How do I add padding/zeros in Python 3 formatting?
    {:0>5} zero-pads to 5 chars — useful in string formatters in Python 3.
  5. Can I use variables and calculations in string formatters in Python 3?
    Yes — f-strings support expressions: f”Total: {price * qty:.2f}”.

Summary

You now know exactly how to use string formatters in Python 3: f-strings (modern standard), str.format(), legacy %, type specifiers, padding/alignment, precision, and best practices.

Mastering how to use string formatters in Python 3 makes your console output, logs, reports, UIs, emails, and data displays clean, professional, and user-friendly — a core daily skill for every Python developer.

Recommended Next Tutorials

  • Python f-Strings Deep Dive (All Formatting Codes)
  • Python String Methods & Operations
  • Python Regular Expressions (re module)
  • Working with Files & Formatted Text
  • Build a CLI Report Generator with Formatters