|
| 1 | + |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# ### Formatting Output with Modulo Operator |
| 5 | + |
| 6 | +# In[1]: |
| 7 | + |
| 8 | + |
| 9 | +print("%10.3e"%(356.08977)) |
| 10 | + |
| 11 | + |
| 12 | +# In[2]: |
| 13 | + |
| 14 | + |
| 15 | +print("%10.3E"%(356.08977)) |
| 16 | + |
| 17 | + |
| 18 | +# In[3]: |
| 19 | + |
| 20 | + |
| 21 | +print("%10o"%(25)) |
| 22 | + |
| 23 | + |
| 24 | +# In[4]: |
| 25 | + |
| 26 | + |
| 27 | +print("%10.5o"%(25)) |
| 28 | + |
| 29 | + |
| 30 | +# In[5]: |
| 31 | + |
| 32 | + |
| 33 | +print("%5x"%(47)) |
| 34 | + |
| 35 | + |
| 36 | +# In[6]: |
| 37 | + |
| 38 | + |
| 39 | +print("%5.4X"%(47)) |
| 40 | + |
| 41 | + |
| 42 | +# In[7]: |
| 43 | + |
| 44 | + |
| 45 | +print("Only one percentage sign: %% " %()) |
| 46 | + |
| 47 | + |
| 48 | +# ### Formatting Output Using String Method "format" |
| 49 | + |
| 50 | +# In[8]: |
| 51 | + |
| 52 | + |
| 53 | +# We don't use print method, instead use .format() |
| 54 | + |
| 55 | +"Art:{a:5d}, Price:{p:8.2f}".format(a=453,p=59.058) |
| 56 | + |
| 57 | + |
| 58 | +# If the width field is preceded by 0 character, sign-aware zero-padding for numeric types will be enabled |
| 59 | + |
| 60 | +# In[9]: |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | +x=378 |
| 65 | +print("The value is {:06d}".format(x)) |
| 66 | + |
| 67 | + |
| 68 | +# In[10]: |
| 69 | + |
| 70 | + |
| 71 | +x=-378 |
| 72 | +print("The value is {:06d}".format(x)) |
| 73 | + |
| 74 | + |
| 75 | +# |
| 76 | +# This option signals the use of a comma for a thousands separator |
| 77 | + |
| 78 | +# In[11]: |
| 79 | + |
| 80 | + |
| 81 | +x=7893512456 |
| 82 | +print("The value is {:,}".format(x)) |
| 83 | + |
| 84 | + |
| 85 | +# In[12]: |
| 86 | + |
| 87 | + |
| 88 | +x=7893512456 |
| 89 | +print("The value is {0:6,d}".format(x)) |
| 90 | + |
| 91 | + |
| 92 | +# In[13]: |
| 93 | + |
| 94 | + |
| 95 | +x=7893512456 |
| 96 | +print("The value is {0:12,.3f}".format(x)) |
| 97 | + |
| 98 | + |
| 99 | +# In[15]: |
| 100 | + |
| 101 | + |
| 102 | +# By default left justify is used , < signifies left and > signifies right |
| 103 | +"{0:<20s} {1:6.2f}".format('Spam & Eggs:',6.99) |
| 104 | + |
| 105 | + |
| 106 | +# In[16]: |
| 107 | + |
| 108 | + |
| 109 | +"{0:>20s} {1:6.2f}".format('Spam & Eggs:',6.99) |
| 110 | + |
| 111 | + |
| 112 | +# In[17]: |
| 113 | + |
| 114 | + |
| 115 | +"{0:20s} {1:6.2f}".format('Spam & Eggs:',6.99) |
| 116 | + |
| 117 | + |
| 118 | +# |
| 119 | +# Examples of formatting output using .format() method of class string |
| 120 | + |
| 121 | +# In[18]: |
| 122 | + |
| 123 | + |
| 124 | +"First argument :{0}, second one: {1}".format(47,11) |
| 125 | + |
| 126 | + |
| 127 | +# In[19]: |
| 128 | + |
| 129 | + |
| 130 | +"Second argument :{1}, first one: {0}".format(47,11) |
| 131 | + |
| 132 | + |
| 133 | +# In[20]: |
| 134 | + |
| 135 | + |
| 136 | +"Second argument :{1:3d}, first one: {0:7.2f}".format(47.42,11) |
| 137 | + |
| 138 | + |
| 139 | +# In[21]: |
| 140 | + |
| 141 | + |
| 142 | +"First argument :{}, second one: {}".format(47,11) |
| 143 | + |
| 144 | + |
| 145 | +# In[22]: |
| 146 | + |
| 147 | + |
| 148 | +"various precisions :{0:6.2f} or {0:6.3f}".format(1.4148) |
| 149 | + |
0 commit comments