print('{0}, {1}'.format('python', '学习日记'))
print('{}, {}'.format('python', '学习日记'))
print('{1}, {0}, {1}'.format('python', '学习日记'))
print('{language}, {dairy}'.format(language = 'python', dairy = '学习日记'))
article = ['Python', '学习日记']
print('{0[0]}, {0[1]}'.format(article))
class Article:
def __init__(self, language, dairy):
self.language, self.dairy = language, dairy
def __str__(self):
return 'this article is {self.language},{self.dairy}'.format(self = self)
print(str(Article('Python', '学习日记')))
print('{0:x<4}'.format(12))
%显示百分数
e显示指数记法
format_spec 的格式
format_spec | [[fill]align][sign][#][0][width][,][.precision][type] |
---|---|
fill | 任意字符 |
align | ”<” , “>” , “=” , “^” |
sign | ”+” , “-” , ” “ |
width | 整型 |
precision | 整型 |
type | ”b” , “c” , “d” , “e” , “E” , “f” , “F” , “g” , “G” , “n” , “o” , “s” , “x” , “X” |
例如
print('{:x<4,}'.format(199992))
f = 'hello {0} i am {1}'.format
print(f('Kevin','Tom'))
print 'hello {0:>{1}} '.format('Kevin',50)