请问python3怎么用format格式化输出字典里的数据
比如像python2这样:data={‘name’:'John','age':18,'height':180}print'Theboynamed%(name)sis%(a...
比如像python2这样:
data = {‘name’:'John','age':18,'height':180}
print 'The boy named %(name)s is %(age)d-year-old and %(height)g tall.' %data 展开
data = {‘name’:'John','age':18,'height':180}
print 'The boy named %(name)s is %(age)d-year-old and %(height)g tall.' %data 展开
展开全部
data = {‘name’:'John','age':18,'height':180}
print('The boy named {0[name]:s} is {0[age]:d}-year-old and {0[height]:g} tall.' .format(data))
如果用format,语法就像上面这样用,{0}表示第一个参数
其实用%号格式化字符串和format两种语法在python3中都是可以使用的
追问
我一直以为python3只能用format,不能用%了呢。。多谢多谢
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询