1. What is String Formatting?

String formatting is the process of inserting values into a string.

  • Makes output more dynamic
  • Improves readability
  • Saves time when generating messages

Key Point:
Formatting = combining variables with text efficiently


2. The .format() Method

The .format() method inserts values into placeholders {} inside a string.

Basic Structure:

"Text {}".format(value)

Example concept:

  • "Hello {}" → insert variable

Key Point:
Curly braces {} mark insertion points


3. Using Multiple Values

You can insert multiple variables:

"{} scored {}".format(name, number)
  • First {} → first argument
  • Second {} → second argument

Key Point:
Order matters by default


4. Named Placeholders

You can assign names inside {}.

"{name} scored {num}".format(name="Alice", num=90)

Benefits:

  • Order of arguments does NOT matter
  • More readable

Key Point:
Named placeholders improve clarity


5. Indexed Placeholders

You can use index numbers:

"{1} scored {0}".format(number, name)
  • {0} → first argument
  • {1} → second argument

Key Point:
Indexing allows flexible ordering


6. Why Formatting is Useful

  • Handles different data types automatically
  • Works with:
    • Strings
    • Integers
    • Floats

Key Point:
No need for manual type conversion


7. Formatting Numbers (Decimal Control)

You can control decimal places.

Syntax:

"{:.2f}".format(value)
  • .2 → two decimal places
  • f → float

Example:

  • 8.29258.29

Key Point:
Control precision for readability


8. Adjusting Decimal Places

You can change precision:

  • .0f → no decimals
  • .3f → three decimals

Key Point:
Flexible numeric formatting


9. Aligning Text

You can align output using formatting.

Syntax:

"{:>3}".format(value)
  • > → right alignment
  • 3 → width

Example:

  • Align numbers in columns

Key Point:
Alignment improves presentation


10. Example: Table Formatting

Use formatting to create clean tables:

  • Align numbers
  • Limit decimals
  • Improve readability

Key Point:
Useful for reports and data output


11. Real-World Use Case

Example:

  • Price calculation with tax

Problem:

  • Too many decimal places

Solution:

  • Format to 2 decimal places

Result:

  • Clean and professional output

Key Point:
Formatting improves data presentation


12. Flexibility for Internationalization

Formatting allows:

  • Reordering variables
  • Supporting different languages

Example:

  • Different word order in translations

Key Point:
Useful for global applications


13. Importance for Data Professionals

String formatting helps:

  • Generate reports
  • Display results clearly
  • Format outputs for stakeholders

Key Point:
Clear output = better communication


Final Summary

The .format() method in Python is a powerful tool for inserting and formatting values within strings. It supports positional, named, and indexed placeholders, allowing flexible control over how data is displayed. Additionally, it enables formatting of numbers, including controlling decimal precision and alignment. This makes it especially useful for creating clean, readable outputs in data analysis and reporting.


Key Takeaways

  • {} = placeholder for values
  • .format() inserts values into strings
  • Order matters unless using names
  • Named placeholders improve readability
  • Indexed placeholders allow reordering
  • Use :.2f for decimal formatting
  • Use > for alignment
  • Essential for clean and professional output