■
Type Meaning
'e' Exponent notation. Prints the number in scientific notation using the letter ‘e’ to indicate the exponent.
'E' Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.
'f' Fixed point. Displays the number as a fixed-point number.
'F' Fixed point. Same as 'f'.
'g' General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. Infinity and NaN values are formatted as inf, -inf and nan, respectively.
'G' General format. Same as 'g' except switches to 'E' if the number gets to large. The representations of infinity and NaN are uppercased, too.
'n' Number. This is the same as 'g', except that it uses the current locale setting to insert the appropriate number separator characters.
'%' Percentage. Multiplies the number by 100 and displays in fixed ('f') format, followed by a percent sign.
None The same as 'g'.
整形仕様〈Python 3.0 版〉
Type | Meaning |
---|---|
b | バイナリー書式(2進数)で出力します。 |
c | 文字(整数を変換して対応する unicode 文字を得る)を出力します。 |
d | 10進数で出力します。 |
o | 8進数で出力します。 |
x | 16進数(小文字を使って)で出力します。 |
X | 16進数(大文字を使って)で出力します。 |
n | 10進数(区切り文字を含めた書式)で出力します。 |
文字列の変換指令〈Python 2.x 版〉
変換指令には、記号「%」に続けて次のものを指定できます。
Type | Meaning |
---|---|
c | 文字(整数を変換して対応する unicode 文字を得る)を出力します。 |
d | 10進数で出力します。 |
o | 8進数で出力します。 |
x | 16進数(小文字を使って)で出力します。 |
X | 16進数(大文字を使って)で出力します。 |
def ex(): for e in "cdoxX": s = "'%%%s'%%78"%e print(">>>",s) print(eval(s)) % python2.6 -i ex.py >>> ex() >>> '%c'%78 N >>> '%d'%78 78 >>> '%o'%78 116 >>> '%x'%78 4e >>> '%X'%78 4E
文字 'N' に対応する文字コード 78 が、各変換指令に従って出力されるのが分かります。
Python はじめました:整形仕様 - 実数
Python.use(better) # Python はじめました《記事一覧》
整形仕様 - 実数
整形仕様〈Python 3.0 版〉
変換フィールドには、コロン「:」に続けて次のものを指定できます。
》作業中です《
Type | Meaning |
---|---|
e, E | 指数書式(小文字 'e')で出力します。科学表記で指数部を区別するのに文字 ‘e’ を使います。 |
E | 指数書式(大文字 'E')で出力します。大文字を使う以外は、e と同じです。 |
f, F | 固定小数点数を使って出力します。 |
g, G | 固定小数書式(大文字 'F')で出力します。大文字を使う以外は、f と同じです。 |
n | 区切り文字を含めた書式で出力します。 |
% | 百分率(per-cent)で出力します。 |
N = 0.123456789 def ex_format(): global N X = 'N' print(">>>",X) eval(compile(X,"_","single")) for e in "eEfFgGn%": X = "'{{0:{0}}}'.format(N)".format(e) print(">>>",X) eval(compile(X,"_","single"))
% python3.0 -i exFloat.py >>> ex_format() >>> N 0.123456789 >>> '{0:e}'.format(N) 1.234568e-01 >>> '{0:E}'.format(N) 1.234568E-01 >>> '{0:f}'.format(N) 0.123457 >>> '{0:F}'.format(N) 0.123457 >>> '{0:g}'.format(N) 0.123457 >>> '{0:G}'.format(N) 0.123457 >>> '{0:n}'.format(N) 0.123457 >>> '{0:%}'.format(N) 12.345679%
変数 N の値(実数)が、各書式に従って出力されるのが分かります。
>>> N*=10000
>>> ex_format()
>>> N
1234.56789
>>> '{0:e}'.format(N)
1.234568e+03
>>> '{0:E}'.format(N)
1.234568E+03
>>> '{0:f}'.format(N)
1234.567890
>>> '{0:F}'.format(N)
1234.567890
>>> '{0:g}'.format(N)
1234.57
>>> '{0:G}'.format(N)
1234.57
>>> '{0:n}'.format(N)
1,234.57
>>> '{0:%}'.format(N)
123456.789000%
>>> N*=10000; do(1)
このとき「:n」を指定すると、3桁ごとに(区切り文字として)カンマ「,」が出力されるのが分かります。
>>> N*=10000
>>> ex_format()
>>> N
12345678.9
>>> '{0:e}'.format(N)
1.234568e+07
>>> '{0:E}'.format(N)
1.234568E+07
>>> '{0:f}'.format(N)
12345678.900000
>>> '{0:F}'.format(N)
12345678.900000
>>> '{0:g}'.format(N)
1.23457e+07
>>> '{0:G}'.format(N)
1.23457E+07
>>> '{0:n}'.format(N)
1.23457e+07
>>> '{0:%}'.format(N)
1234567890.000000%
このとき「:n」を指定すると、3桁ごとに(区切り文字として)カンマ「,」が出力されるのが分かります。