pythonのフォーマット出力
5168 ワード
文字列のフォーマットには、%とformatの2つの方法があります.
まず%号の方法を紹介します
まず%号の方法を紹介します
#%s , , ,
# , %s
# s = ' %s, %d' %('alex',2)
# print(s)
# alex, 2
#%[(name)][flags][width].[precision]typecode
# [(name)], % , name, name , ,
#
# s = ' %(name)s, %(age)d' %{'name':'alex','age':12}
# print(s)
# alex, 12
#flags, + - 0, width ,%
#+ , , + , -
# s = 'my age is %(age1)+10dDDDD,my brother age is%(age2)+10d' %{'age1':12,'age2':-12}
# print(s)
# my age is +12DDDD,my brother age is -12
#- , , , -
# s = 'my age is %(age1)-10dDDDD,my brother age is%(age2)-10d' %{'age1':12,'age2':-12}
# print(s)
# my age is 12 DDDD,my brother age is-12
#0 , , , - , 0
# , , , -
#width,
#precision ,
# s = 'my age is %(age1)-10fDDDD,my brother age is%(age2)-10d' %{'age1':12.1234445,'age2':-12}
# print(s)
# my age is 12.123444 DDDD,my brother age is-12
# s = 'my age is %(age1)-10.4fDDDD,my brother age is%(age2)-10d' %{'age1':12.1234445,'age2':-12}
# print(s)
# my age is 12.1234 DDDD,my brother age is-12
# typecode
#s:
#o: ,
#x: 16 ,
#d,
#f,
# s = 'aaaaaadd %d------------%o----------%x' %(15,15,15)
# print(s)
# aaaaaadd 15------------17----------f
#e|E , e, E
# s = '%e--------------%E' %(100000000000000000000,100000000000000000)
# print(s)
# 1.000000e+20--------------1.000000E+17
#%
# s = 'football %'
# print(s)
# s = 'football %%'
# print(s)
# football %
# football %%
# s = 'name is %s,%' %('dddd')
# print(s)
#
# s = 'name is %s,%%' %('dddd')
# print(s)
# name is dddd,%
format
# [(fill)align][sign][#][0][width][,][.precision][type]
#fill:【 】 ,
#align, , width
##>:
#=:
#^:
#sign 【 】 、
# + ,
#- ,
# , ,
# # ,【 】 , ,16 , # , 0b 0o 0x, ,
#, , , 100,000,,000
#width,
#.precision,
#type,
#b,
#format ,
# s = 'aaaaaaaadd{0},dddd{0}----{1}' .format('AAA','BBBB')
# print(s)
# aaaaaaaaddAAA,ddddAAA----BBBB
#
# s = '-------{name:s},-------{age:d}------{name:s}' .format(name = 'AAA',age = 23)
# print(s)
# -------AAA,-------23------AAA
#[(fill)align][sign][#][0][width][,][.precision][type], , , name
# s = '-------------{:@^10s}-----------' .format('AAA')
# print(s)
# -------------@@@AAA@@@@-----------
# s = '-------------{name:@^10s}-----------' .format(name = 'AAA')
# print(s)
# -------------@@@AAA@@@@-----------
# s = '--------{:%}' .format(1.345566677)
# print(s)
# --------134.556668%
#format
# s = 'i am {},age {},{}' .format('foot',12,'boy')
# print(s)
# i am foot,age 12,boy
# s = 'i am {},age {},{}' .format(*['foot',12,'boy'])
# print(s)
# i am foot,age 12,boy
# s = 'i am {0},age {1},{1}' .format('foot',12)
# print(s)
# i am foot,age 12,12
# s = 'i am {0},age {1},{1}' .format(*['foot',12])
# print(s)
# i am foot,age 12,12
# s = 'i am {name},age is {age}' .format(name = 'foot',age = 12)
# print(s)
# i am foot,age is 12
# s = 'i am {name},age is {age}' .format(**{'name':'football','age':12})
# print(s)
# i am football,age is 12
# s = 'i am {name:s},age is {age:d}' .format(name = 'foot',age = 12)
# print(s)
# i am foot,age is 12
# s = '--{:b}--{:d}--{:x}--{:X}--{:%}' .format(15,15,15,15,15)
# print(s)
# --1111--15--f--F--1500.000000%
# s = '--{0:b}--{0:d}--{0:x}--{0:X}--{0:%}' .format(15)
# print(s)
# --1111--15--f--F--1500.000000%
s = '--{num:b}--{num:d}--{num:x}--{num:X}--{num:%}' .format(num = 15)
print(s)
# --1111--15--f--F--1500.000000%