-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormat specifiers .py
More file actions
74 lines (47 loc) · 1.76 KB
/
Copy pathFormat specifiers .py
File metadata and controls
74 lines (47 loc) · 1.76 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
# Format specifiers = {value:flags} format a value based on what flags are inserted
# .(number)f = round to that many decimal places (fixed point)
# :(number) = allocate that many spaces
# :03 = alllocate and zero pad that many spaces
# :< = left justify
# :> = right justify
# :^ = center align
# :+ = use a plus sign to indicate +ve value
# := = place sign to leftmost position
# : = insert a space before positive numbers
# :, = comma separator
# Note that mixture of 2 or more flags is allowed based on what you are looking for or what you need to achieve
price1 = 3.14159
price2 = -987.65
price3 = 12.34
# reducing or increasing the decimal places (.(number)f)
'''print(f"Price 1 is ${price1:.2f}")
print(f"Price 2 is ${price2:.2f}")
print(f"Price 3 is ${price3:.2f}")'''
# allocating a number of spaces
'''print(f"Price 1 is ${price1:10}")
print(f"Price 2 is ${price2:10}")
print(f"Price 3 is ${price3:10}")'''
# zero padding
'''print(f"Price 1 is ${price1:010}")
print(f"Price 2 is ${price2:010}")
print(f"Price 3 is ${price3:010}")'''
# left align
'''print(f"Price 1 is ${price1:<}")
print(f"Price 2 is ${price2:<}")
print(f"Price 3 is ${price3:<}")'''
# right align
'''print(f"Price 1 is ${price1:>10}")
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}")'''
# to display a plus sign to indicate +ve value. A space can also be used or even comma for thousand separation
'''print(f"Price 1 is ${price1:+}")
print(f"Price 2 is ${price2:+}")
print(f"Price 3 is ${price3:+}")'''
# to assign to the leftmost position
'''print(f"Price 1 is ${price1:=}")
print(f"Price 2 is ${price2:=}")
print(f"Price 3 is ${price3:=}")'''