标签:font print pytho form log his 格式化输出 格式化 操作
1."{},{}".format(,)用法总结:
‘{0},{1}‘.format(‘var1‘,132908) ‘var1,132908‘ ‘{},{}‘.format(‘var1‘,132908) ‘var1,132908 ‘{0},{1},{1},{0}‘.format(‘var1‘,132908) ‘var1,132908,132908,var1‘
2.关键字参数:
‘{name},{age}‘.format(age=18,name=‘wangchao‘) ‘wangchao,18‘ class Person: def __init__(self,name,age): self.name,self.age = name,age def __str__(self): return ‘This book is {self.name},is {self.age} old‘.format(self=self) print Person(‘wangdage‘,18) This book is wangdage,is 18 old p=[‘wanghhao‘,18] ‘{0[0]},{0[1]}‘.format(p) ‘wanghhao,18‘
3."%s"%()形式:
print"I‘m %s. I‘m %d year old" % (‘cobe‘, 99) I‘m cobe. I‘m 99 year old print "I‘m %(name)s. I‘m %(age)d year old" % {‘name‘:‘coolbe‘, ‘age‘:99} I‘m coolbe. I‘m 99 year old
总结:
Python中除了内置的%操作符可用于格式化字符串操作,还有"".format()的形式,都需要熟练掌握
标签:font print pytho form log his 格式化输出 格式化 操作
原文地址:http://www.cnblogs.com/wc554303896/p/7074442.html