String formatting produces readable output from a format string and supplied values. In Python, f-strings are the preferred approach for interpolating expressions, including integers and floats, because they avoid cumbersome concatenation and explicit conversions. section 2.1 section 2.1.1
Create formatted task output
name = "Homework" urgency = 5 task = f"Name: {name}; Urgency Level: {urgency}" print(task) # Name: Homework; Urgency Level: 5 section 2.1
Choose formatting patterns according to the output requirement:
- Use expressions inside curly braces: f"{name} has urgency {urgency}". section 2.1
- Add a colon and format specifier to control alignment, padding, numeric bases, precision, scientific notation, or percentages. section 2.1.4
- Prefer f-strings over older percent formatting and str.format(); both remain supported but are described as obsolete for typical use. section 2.1.1
- Refactor repeated format specifications into a function when the same layout appears throughout the code. section 2.1.4
