Skip to content

Commit cee21e7

Browse files
committed
Sample code for the article on string formatting tools
1 parent f5140c8 commit cee21e7

File tree

4 files changed

+206
-0
lines changed

4 files changed

+206
-0
lines changed

python-formatted-output/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# A Guide to the Newer Python String Format Techniques
2+
3+
This folder provides the code examples for the Real Python tutorial [A Guide to the Newer Python String Format Techniques](https://realpython.com/python-formatted-output/).

python-formatted-output/format.py

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
print(f"{257:b}")
2+
print("{:b}".format(257))
3+
4+
print(f"{42:c}")
5+
print("{:c}".format(42))
6+
7+
print(f"{3.14159:g}")
8+
print(f"{-123456789.8765:g}")
9+
print("{:g}".format(3.14159))
10+
print("{:g}".format(-123456789.8765))
11+
12+
print(f"{-123456789.8765:G}")
13+
print("{:G}".format(-123456789.8765))
14+
15+
infinite = 1e300 * 1e300
16+
not_a_number = infinite * 0
17+
print(f"{infinite:g}")
18+
print(f"{not_a_number:g}")
19+
print(f"{infinite:G}")
20+
print(f"{not_a_number:G}")
21+
print("{:g}".format(infinite))
22+
print("{:g}".format(not_a_number))
23+
print("{:G}".format(infinite))
24+
print("{:G}".format(not_a_number))
25+
26+
print(f"{'Hi':8s}")
27+
print(f"{123:8d}")
28+
print("{0:8s}".format("Hi"))
29+
print("{0:8d}".format(123))
30+
31+
print("{0:2s}".format("Pythonista"))
32+
33+
print(f"{'Hi':<8s}")
34+
print(f"{123:<8d}")
35+
print("{0:<8s}".format("Hi"))
36+
print("{0:<8d}".format(123))
37+
38+
print(f"{'Hi':>8s}")
39+
print(f"{123:>8d}")
40+
print("{0:>8s}".format("Hi"))
41+
print("{0:>8d}".format(123))
42+
43+
print(f"{'Hi':^8s}")
44+
print(f"{123:^8d}")
45+
print("{0:^8s}".format("Hi"))
46+
print("{0:^8d}".format(123))
47+
48+
print(f"{123:+8d}")
49+
print(f"{-123:+8d}")
50+
print("{0:+8d}".format(123))
51+
print("{0:+8d}".format(-123))
52+
53+
print(f"{123:=+8d}")
54+
print(f"{-123:=+8d}")
55+
print("{0:=+8d}".format(123))
56+
print("{0:=+8d}".format(-123))
57+
58+
print(f"{'Hi':->8s}")
59+
print(f"{123:#<8d}")
60+
print(f"{'Hi':*^8s}")
61+
print("{0:->8s}".format("Hi"))
62+
print("{0:#<8d}".format(123))
63+
print("{0:*^8s}".format("Hi"))
64+
65+
print(f"{123:+6d}")
66+
print(f"{-123:+6d}")
67+
print("{0:+6d}".format(123))
68+
print("{0:+6d}".format(-123))
69+
70+
print(f"{123:-6d}")
71+
print(f"{-123:-6d}")
72+
print("{0:-6d}".format(123))
73+
print("{0:-6d}".format(-123))
74+
75+
print(f"{123:*> 6d}")
76+
print(f"{-123:*> 6d}")
77+
print("{0:*> 6d}".format(123))
78+
print("{0:*> 6d}".format(-123))
79+
80+
value = 16
81+
print(f"{value:b}, {value:#b}")
82+
print(f"{value:o}, {value:#o}")
83+
print(f"{value:x}, {value:#x}")
84+
print("{0:b}, {0:#b}".format(value))
85+
print("{0:o}, {0:#o}".format(value))
86+
print("{0:x}, {0:#x}".format(value))
87+
88+
print(f"{123:.0f}, {123:#.0f}")
89+
print(f"{123:.0e}, {123:#.0e}")
90+
print("{0:.0f}, {0:#.0f}".format(123))
91+
print("{0:.0e}, {0:#.0e}".format(123))
92+
93+
print(f"{123:05d}")
94+
print(f"{12.3:08.1f}")
95+
print("{0:05d}".format(123))
96+
print("{0:08.1f}".format(12.3))
97+
98+
print(f"{'Hi':>06s}")
99+
print("{0:>06s}".format("Hi"))
100+
101+
print(f"{123:*>05d}")
102+
print("{0:*>05d}".format(123))
103+
104+
print(f"{1234567:,d}")
105+
print(f"{1234567:_d}")
106+
print(f"{1234567.89:,.2f}")
107+
print(f"{1234567.89:_.2f}")
108+
print("{0:,d}".format(1234567))
109+
print("{0:_d}".format(1234567))
110+
print("{0:,.2f}".format(1234567.89))
111+
print("{0:_.2f}".format(1234567.89))
112+
113+
print(f"{0b111010100001:_b}")
114+
print(f"{0b111010100001:#_b}")
115+
print(f"{0xae123fcc8ab2:_x}")
116+
print(f"{0xae123fcc8ab2:#_x}")
117+
print("{0:_b}".format(0b111010100001))
118+
print("{0:#_b}".format(0b111010100001))
119+
print("{0:_x}".format(0xAE123FCC8AB2))
120+
print("{0:#_x}".format(0xAE123FCC8AB2))
121+
122+
print(f"{1234.5678:8.2f}")
123+
print(f"{1.23:8.4f}")
124+
print(f"{1234.5678:8.2e}")
125+
print(f"{1.23:8.4e}")
126+
print("{0:8.2f}".format(1234.5678))
127+
print("{0:8.4f}".format(1.23))
128+
print("{0:8.2e}".format(1234.5678))
129+
print("{0:8.4e}".format(1.23))
130+
131+
print(f"{'Pythonista':.6s}")
132+
print("{0:.6s}".format("Pythonista"))
133+
134+
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+
print(
139+
"{number:{width}.{prec}f}".format(width=width, prec=prec, number=123.4567)
140+
)
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
print(f"Hello, Pythonista!")
2+
print(f"Hello, Pythonista!")
3+
4+
print(f"Single-line f-string with single quotes")
5+
print(f"Single-line f-string with double quotes")
6+
print(
7+
f"""Multiline triple-quoted f-string
8+
with single quotes"""
9+
)
10+
print(
11+
f"""Multiline triple-quoted f-string
12+
with double quotes"""
13+
)
14+
15+
site = "Real Python"
16+
print(f"Welcome to {site}!")
17+
18+
quantity = 6
19+
item = "bananas"
20+
price = 1.74
21+
print(f"{quantity} {item} cost ${price * quantity}")
22+
23+
fruits = ["apple", "mango", "grape"]
24+
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:]}")
27+
print(f"Dict value for key 'one' is {numbers['one']}")
28+
29+
lang = "Python"
30+
print(f"{{ {lang} }}")
31+
32+
print("{0} {1} cost ${2}".format(6, "bananas", 1.74 * 6))
33+
34+
print("{2}.{1}.{0}/{0}{0}.{1}{1}.{2}{2}".format("foo", "bar", "baz"))
35+
36+
print("{} {} cost ${}".format(6, "bananas", 1.74 * 6))
37+
38+
print(
39+
"{quantity} {item} cost ${cost}".format(
40+
quantity=6, item="bananas", cost=1.74 * 6
41+
)
42+
)
43+
44+
print("{x} {y} {z}".format(x="foo", y="bar", z="baz"))
45+
46+
print("{x} {y} {z}".format(x="foo", y="bar", z="baz"))

python-formatted-output/person.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Person:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def __str__(self):
7+
return f"I'm {self.name}, and I'm {self.age} years old."
8+
9+
def __repr__(self):
10+
return f"{type(self).__name__}(name='{self.name}', age={self.age})"
11+
12+
13+
jane = Person("Jane", 25)
14+
f"{jane!s}"
15+
f"{jane!r}"
16+
"{jane!s}".format(jane=jane)
17+
"{jane!r}".format(jane=jane)

0 commit comments

Comments
 (0)