-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathString_formatting_1.py
188 lines (96 loc) · 6.68 KB
/
String_formatting_1.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# source - https://realpython.com/python-input-output/#the-string-modulo-operator
'''
d, i, u Decimal integer
x, X Hexadecimal integer
o Octal integer
f, F Floating point
e, E Exponential
g, G Floating point or Exponential
c Single character
s, r, a String
% Single '%' character
'''
'%x, %X' % (252, 252)
'fc, FC'
'%o' % 16
'20'
print(f"{3.14:><20F} { 3.14:10.2f}")
print('%e, %E' % (1000.0, 1000.0))
# prints -'1.000000e+03, 1.000000E+03'
# The g and G conversion types choose between floating point or exponential output, depending on the magnitude of the exponent
print('%g' % 3.14)
# '3.14'
print('%g' % 0.00000003)
# '3e-08'
print('%G' % 0.00000003)
# '3E-08'
# ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# Deep Dive: inf and NaN
# Under some circumstances, a floating-point operation can result in a value that is essentially infinite. The string representation of such a number in Python is 'inf'.
#
# It also may happen that a floating-point operation produces a value that is not representable as a number. Python represents this with the string 'NaN'.
#
# When these values are converted with the string modulo operator, the conversion type character controls the case of the resulting output. f and e produce lowercase output, while F and E produce uppercase:
x = float('NaN')
print('%f, %e, %F, %E' % (x, x, x, x))
'nan, nan, NAN, NAN'
y = float('Inf')
print('%f, %e, %F, %E' % (y, y, y, y))
'inf, inf, INF, INF'
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# '%*d' % (5, 123)
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# >>> '%#o' % 16
# '0o20'
#
# >>> '%#x' % 16, '%#X' % 16
# ('0x10', '0X10')
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# >>> '%.0f' % 123
# '123'
# >>> '%#.0f' % 123
# '123.'
#
# >>> '%.0e' % 123
# '1e+02'
# >>> '%#.0e' % 123
# '1.e+02'
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# >>> '%-5d' % 123
# '123 '
#
# >>> '%-8.2f' % 123.3
# '123.30 '
#
# >>> '%-*s' % (10, 'foo')
# 'foo '
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# The + and ' ' Flags
# >>> '%+d' % 3
# '+3'
# >>> '%+5d' % 3
# ' +3'
# >>> '% d' % 3
# ' 3'
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# >>> d = {'quantity': 6, 'item': 'bananas', 'price': 1.74}
#
# >>> '%(quantity)d %(item)s cost $%(price).2f' % d
# '6 bananas cost $1.74'
#
# >>> 'It will cost you $%(price).2f for %(item)s, if you buy %(quantity)d' % d
# 'It will cost you $1.74 for bananas, if you buy 6'
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# 'Get %d%% off on %s today only!' % (30, 'bananas')
# 'Get 30% off on bananas today only!'
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
# ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------