Repository navigation
Expand file tree
/
Copy path10_format_specifiers.py
More file actions
73 lines (54 loc) · 2.47 KB
/
Copy path10_format_specifiers.py
File metadata and controls
73 lines (54 loc) · 2.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# format specifiers = {value:flags} when used in the context of f-string, they allow
# us to format a value based on what flags are inserted
# .(number)f = round to that many decimal places (fixed point)
# :(number) = Reserve a minimum width of that many characters.
# Python will never cut off the value if the value itself is longer.
# :03 = allocate and zero pad that many spaces
# :< = left justify
# :> = right justify
# :^ = center align
# :+ = Always show the sign (+ or -)
# : = insert a space before positive numbers
# :, = comma separator
price1 = 3000.14159
price2 = -9870.65
price3 = 1200.34
print(f"Price 1 is ${price1:.2f}") # it will display 2 decimal places
print(f"Price 2 is ${price2:.1f}") # it will display 1 decimal place
print(f"Price 3 is ${price3:.3f}") # it will display 3 decimal places
# to allocate space to display a value
# below each value has a total of 10 spaces to display the output
print(f"Price 1 is ${price1:10}")
print(f"Price 2 is ${price2:10}")
print(f"Price 3 is ${price3:10}")
# if you want to precede number with 0 , now these numbers will be 0 padded
print(f"Price 1 is ${price1:010}")
print(f"Price 2 is ${price2:010}")
print(f"Price 3 is ${price3:010}")
# to left justify a number
print(f"Price 1 is ${price1:<10}") # now we will have all space left after number
print(f"Price 2 is ${price2:<10}")
print(f"Price 3 is ${price3:<10}")
# to right justify a number
print(f"Price 1 is ${price1:>10}") # now we will have all space left before number
print(f"Price 2 is ${price2:>10}")
print(f"Price 3 is ${price3:>10}")
# Center align
print(f"Price 1 is ${price1:^10}")
print(f"Price 2 is ${price2:^10}")
print(f"Price 3 is ${price3:^10}")
# If you want to display a sign with a number
print(f"Price 1 is ${price1:+}") # Positive numbers get a + sign
print(f"Price 2 is ${price2:+}") # Negative numbers keep their - sign
print(f"Price 3 is ${price3:+}")
print(f"Price 1 is ${price1: }") # Or Leave a blank space for positive numbers, but keep the - sign for negative numbers
print(f"Price 2 is ${price2: }")
print(f"Price 3 is ${price3: }")
# there is (1,000)thousand separator which is a comma
print(f"Price 1 is ${price1:,}")
print(f"Price 2 is ${price2:,}")
print(f"Price 3 is ${price3:,}")
# We could also mix and match flags
print(f"Price 1 is ${price1:+,.2f}")
print(f"Price 2 is ${price2:+,.2f}")
print(f"Price 3 is ${price3:+,.2f}")