python 如何往空列表里添加元素_Python笔记_循环

1、for循环-遍历循环

①正常情况下

costs = [5,7,29,30,40,88,99,34]

for cost in costs:

   print('花费 {} 元 '.format(cost))

运行结果

6dbeb32664a87ac070f926c3f92383f2.png

②添加center函数,需要把cost先变成字符串格式

costs = [5,7,29,30,40,88,99,34]

for cost in costs:

   print('花费 {} 元 '.format(str(cost).center(15))) ##str把cost转换成字符串格式

运行结果:

3454152c0bb39af10100308a3ca9ca7a.png

③center居中函数需要在format函数里面,数据随便调整

costs = [5,7,29,30,40,88,99,34]

for cost in costs:

   print('花费是{}元 '.format(str(cost).center(10)))

运行结果:


版权声明:本文为weixin_39830387原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。