Pylerno
English

String formatting

Format values for people without changing the underlying data.

On this page

The concept

An f-string evaluates expressions inside braces and inserts their formatted values into text. This avoids confusing chains of string conversion and concatenation. A format specification such as :.2f displays two fractional digits. It affects presentation, not the value stored in the numeric variable.

print also accepts sep and end. sep controls the separator between arguments; end controls what is emitted after the call. They are useful for small text reports. Formatting is not validation, and showing two decimal places does not make floating-point arithmetic exact. Decimal is introduced later for explicit decimal calculations.

Worked example

python
name = "Dana"
price = 19.956
print("Python", "for", "everyone", sep=" | ")
print("Part one", end=" ... ")
print("Part two")
print(f"Name: {name}, price: {price:.2f}")
print("First line\nSecond line")
print(f"Sum of 3 and 4 = {3 + 4}")

Trace the result

  1. The separator example inserts a vertical bar between multiple arguments.
  2. The end argument keeps the next call on the same output line.
  3. The f-string reads name and price, then formats price to two decimal places without rebinding price.
text
Python | for | everyone
Part one ... Part two
Name: Dana, price: 19.96
First line
Second line
Sum of 3 and 4 = 7
Line-by-line walkthrough

Line 1: name = "Dana"

Evaluate 'Dana', then bind or update name with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 2: price = 19.956

Evaluate 19.956, then bind or update price with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.

Line 3: print("Python", "for", "everyone", sep=" | ")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 4: print("Part one", end=" ... ")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 5: print("Part two")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 6: print(f"Name: {name}, price: {price:.2f}")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 7: print("First line\nSecond line")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Line 8: print(f"Sum of 3 and 4 = {3 + 4}")

Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.

Common mistake

Formatted numbers are strings. Keep numeric values available for calculations and format only at the display boundary.

Check your understanding

What does this program print?

python
print(f"{3.5:.2f}")
Choose one answer

Search documentation

Share this page