# Nakafa Framework: LLM URL: /en/subject/university/bachelor/ai-ds/ai-programming/string-formatting Source: https://raw.githubusercontent.com/nakafaai/nakafa.com/refs/heads/main/packages/contents/subject/university/bachelor/ai-ds/ai-programming/string-formatting/en.mdx Output docs content for large language models. --- export const metadata = { title: "String Formatting", description: "Master Python string formatting with format(), f-strings, width control, alignment options, and sign specification. Complete guide with tested examples.", authors: [{ name: "Nabil Akbarazzima Fatih" }], date: "09/17/2025", subject: "AI Programming", }; ## Basic Concepts and Width Control String formatting controls data display in Python programs. Imagine arranging data in a neat table where each column has consistent width. Python provides `format()` method and f-strings to control output display. The basic replacement field syntax is `{name : width}`, where `name` determines the argument and `width` sets the minimum output width. When width is smaller than the original string length, Python still displays the complete string. ### Format Types for Number Systems | Format Type | Symbol | Example Input | Output | Usage | |-------------|---------|--------------|--------|-------| | Decimal | `d` | `45` | `45` | Default for integer | | Binary | `b` | `45` | `101101` | Bit representation | | Octal | `o` | `45` | `55` | Unix systems | | Hexadecimal | `x/X` | `45` | `2d/2D` | Low-level programming | ## Position Settings and Fill Characters ### Alignment Options B["Alignment Choice"] B --> C["< (Left)"] B --> D["> (Right)"] B --> E["^ (Center)"] C --> F["Output:
'xxx '"] D --> G["Output:
' xxx'"] E --> H["Output:
' xxx '"] `} /> | Symbol | Name | Behavior | Default for | |--------|------|----------|-------------| | `<` | Left | Text on left, padding on right | String, objects | | `>` | Right | Text on right, padding on left | Numbers | | `^` | Center | Text in center, even padding | - | ### Fill Characters and Advanced Formatting Fill character determines the character to fill empty space. Default uses space, but can be replaced with any character except `{` and `}`. 15}|'.format('xxx')) # Output: | xxx| print('|{0:^15}|'.format('xxx')) # Output: | xxx | # Custom fill characters print('|{0:-<15}|'.format('xxx')) # Output: |xxx------------| print('|{0:*>15}|'.format('xxx')) # Output: |************xxx| print('|{0:^^15}|'.format('xxx')) # Output: |^^^^^^xxx^^^^^^| # Decimal formatting x = 123.98 print('x = {0:12f}'.format(x)) # Output: x = 123.980000 print('x = {0:12e}'.format(x)) # Output: x = 1.239800e+02` } ]} /> ## Argument Reference Methods Python provides three ways to reference arguments in replacement fields, each with different rules. B["Argument Reference Methods"] B --> C["Numbered Fields
{0} {1} {2}"] B --> D["Keyword Fields
{name} {age}"] B --> E["Automatic Fields
{} {} {}"] C --> F["✓ Explicit control
✓ Can repeat
✗ Must match count"] D --> G["✓ Easy to read
✓ Flexible order
✗ Names must match"] E --> H["✓ Simple syntax
✓ Sequential order
✗ Cannot mix with numbered"] `} /> ### Reference Method Comparison | Method | Syntax | Advantages | Limitations | |--------|---------|-----------|-------------| | Numbered | `{0}, {1}` | Explicit control, can repeat | Must match argument count | | Keyword | `{name}, {age}` | Easy to read, flexible | Names must match | | Automatic | `{}, {}` | Simple syntax | Cannot mix with numbered | ## Precision Control and Sign Specification ### Precision for Decimal Numbers Precision controls the number of decimal places or significant digits in number display. | Format Type | Precision Behavior | Example | |-------------|-------------------|---------| | `f/F` | Decimal places | `{:.2f}` → `123.99` | | `e/E` | Decimal places in exponential | `{:.2e}` → `1.24e+02` | | `g/G` | Significant digits | `{:.3g}` → `124` | ### Sign Options B{"Sign Choice"} B --> C["+ (always show)"] B --> D["- (default)"] B --> E["space (for positive)"] C --> F["Output:
+100, -200"] D --> G["Output:
100, -200"] E --> H["Output:
100, -200"] `} /> | Symbol | Behavior | Example | |--------|----------|---------| | `+` | Always show sign | `+100`, `-200` | | `-` | Only negative sign (default) | `100`, `-200` | | ` ` (space) | Space for positive | ` 100`, `-200` | ## F-Strings and Dynamic Templates ### F-Strings (Python 3.6+) F-strings provide the simplest syntax with direct expression evaluation. Faster and more readable compared to `format()`. ### Dynamic Format Templates Format templates can use variables for specification, useful when format is determined dynamically. ### Complete Format Specification Pattern B["Component Order"] B --> C[": (start)"] C --> D["fill (fill character)"] D --> E["align (position: < > ^)"] E --> F["sign (sign: + - space)"] F --> G["width (minimum width)"] G --> H[".prec (decimal precision)"] H --> I["type (type: d b o x f e g)"] J["Complete Example"] --> K["{value:*^+10.2f}"] K --> L["* = fill character
^ = center align
+ = always show sign
10 = width minimum
.2 = 2 decimal places
f = float type"] `} /> Format specification follows the pattern `{name:fill align sign width .prec type}` where each component is optional and has a specific function to control output display.