You have 3 free guides left 😟
Unlock your guides
You have 3 free guides left 😟
Unlock your guides

8.4 String formatting

2 min readjune 24, 2024

in Python offers powerful tools for creating dynamic text. From simple placeholders to precise numerical formatting, it allows for flexible and readable code. Understanding these techniques is crucial for effective string manipulation and presentation.

The method, alignment specifications, and precision controls provide fine-grained control over output. Mastering these concepts enables programmers to generate well-formatted strings for various applications, from user interfaces to data reporting.

String Formatting

String templates with format() method

  • Create string templates with placeholders for values inserted later using marked by curly braces
    [{}](https://www.fiveableKeyTerm:{})
    • Replacement fields can be empty
      {}
      or contain a field name
      {field_name}
  • Insert values into placeholders using the
    format()
    method
    • Pass values as arguments to
      format()
      in the order corresponding to the placeholders
    • Use keyword arguments if field names are used in the replacement fields
  • Example:
    "Hello, {}! My name is {}.".format("Alice", "Bob")
    outputs "Hello, Alice! My name is Bob."
  • can also be achieved using ###-strings_0###, which are a more concise way to embed expressions inside

Alignment and width in string formatting

  • Control formatting of inserted values using alignment and width specifications inside replacement fields
  • Specify alignment using
    [<](https://www.fiveableKeyTerm:<)
    (left-align),
    [^](https://www.fiveableKeyTerm:^)
    (center-align), or
    [>](https://www.fiveableKeyTerm:>)
    (right-align) characters
    • Default alignment is left-aligned
  • Specify width using an integer value representing the minimum number of characters the value should occupy
    • Shorter values are padded with spaces, longer values are not truncated
  • Example:
    "Left: {:<10} | Center: {[:^](https://www.fiveableKeyTerm::^)10} | Right: {[:>](https://www.fiveableKeyTerm::>)10}".format("Apple", "Banana", "Cherry")
    outputs "Left: Apple | Center: Banana | Right: Cherry"

Numerical formatting with precision specifications

  • Specify precision and type for numerical values inside replacement fields
  • Specify precision using
    [.n](https://www.fiveableKeyTerm:.n)
    syntax, where
    n
    is the number of decimal places
    • Floating-point numbers are rounded to the specified precision
    • Precision has no effect on integers
  • Specify type using a type specifier character after the precision
    • f
      : Fixed-point notation (default for floating-point)
    • [e](https://www.fiveableKeyTerm:e)
      : Exponential notation
    • [g](https://www.fiveableKeyTerm:g)
      : General format (uses fixed-point for small numbers, exponential for large numbers)
    • [d](https://www.fiveableKeyTerm:d)
      : Decimal integer (default for integers)
  • Example:
    "Fixed: {:.2f}, Exponential: {:.2e}, General: {:.2g}".format(3.14159)
    outputs "Fixed: 3.14, Exponential: 3.14e+00, General: 3.1"

Additional Formatting Techniques

  • : An older method using
    %
    operator for string formatting
  • String literals can include to represent special characters or formatting
  • F-strings allow for direct embedding of expressions inside string literals, providing a more readable and concise syntax for string interpolation
© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.


© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.

© 2024 Fiveable Inc. All rights reserved.
AP® and SAT® are trademarks registered by the College Board, which is not affiliated with, and does not endorse this website.
Glossary
Glossary