|
| 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 = float("inf") |
| 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 | +) |
0 commit comments