Skip to content

Commit 3ed2087

Browse files
Merge pull request #838 from realpython/maintenance/python-formatted-output-20260916
Sync python-formatted-output with the updated tutorial
2 parents baf97d6 + 634552b commit 3ed2087

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

‎python-formatted-output/format.py‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@
110110
print("{0:,.2f}".format(1234567.89))
111111
print("{0:_.2f}".format(1234567.89))
112112

113+
print(f"{1.23456789:.8_f}")
114+
print("{0:.8_f}".format(1.23456789))
115+
113116
print(f"{0b111010100001:_b}")
114117
print(f"{0b111010100001:#_b}")
115118
print(f"{0xAE123FCC8AB2:_x}")
@@ -132,9 +135,11 @@
132135
print("{0:.6s}".format("Pythonista"))
133136

134137
width = 10
135-
prec = 2
136-
print(f"{123.4567:{width}.{prec}f}")
137-
print("{2:{0}.{1}f}".format(width, prec, 123.4567))
138+
precision = 2
139+
print(f"{123.4567:{width}.{precision}f}")
140+
print("{2:{0}.{1}f}".format(width, precision, 123.4567))
138141
print(
139-
"{number:{width}.{prec}f}".format(width=width, prec=prec, number=123.4567)
142+
"{number:{width}.{precision}f}".format(
143+
width=width, precision=precision, number=123.4567
144+
)
140145
)

‎python-formatted-output/interpolation.py‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# print(f'Hello, Pythonista!')
21
# print(f"Hello, Pythonista!")
2+
# print(F"Hello, Pythonista!")
33

44
# print(f'Single-line f-string with single quotes')
55
# print(f"Single-line f-string with double quotes")
@@ -22,8 +22,8 @@
2222

2323
fruits = ["apple", "mango", "grape"]
2424
numbers = {"one": 1, "two": 2}
25-
print(f"First fruit in list is '{fruits[0]}'")
26-
print(f"Last two fruits in list are {fruits[-2:]}")
25+
print(f"First fruit in the list is '{fruits[0]}'")
26+
print(f"Last two fruits in the list are {fruits[-2:]}")
2727
print(f"Dict value for key 'one' is {numbers['one']}")
2828

2929
lang = "Python"
@@ -43,4 +43,10 @@
4343

4444
print("{x} {y} {z}".format(x="foo", y="bar", z="baz"))
4545

46-
print("{x} {y} {z}".format(x="foo", y="bar", z="baz"))
46+
print("{x} {y} {z}".format(y="bar", z="baz", x="foo"))
47+
print("{y} {z} {x}".format(x="foo", y="bar", z="baz"))
48+
49+
x = "foo"
50+
y = "bar"
51+
z = "baz"
52+
print("{0} {1} {s}".format(x, y, s=z))

0 commit comments

Comments
 (0)