zoukankan      html  css  js  c++  java
  • 大话Python格式化输出字符串

    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()的形式,都需要熟练掌握




  • 相关阅读:
    学习进度
    毕设进度
    学习进度
    毕设进度
    学习进度
    学习进度
    第一周冲刺评论总结&&针对评论总结的改进
    第一阶段成果展示
    团队冲刺--Seven
    团队冲刺--six
  • 原文地址:https://www.cnblogs.com/wc554303896/p/7074442.html
Copyright © 2011-2022 走看看