美文网首页
python print 用法

python print 用法

作者: 仰天望海 | 来源:发表于2021-04-01 16:17 被阅读0次

python code

### 格式化输出
s1 = "hello world"
s2 = 14000

print("%s--%d" % (s1,s2))


### 输出宽度和精度
pai = 3.1415926
print("%d,%10.3f" % (s2,pai))   ###精度为3,指定宽度为10 不足部分用空格补充   14000,     3.142

print("%-10.3f,%d" % (pai,s2))   ### 3.142     ,14000

print("%d,%010.3f" % (s2,pai)) #字段宽度为10,精度为3,不足处用0填充空白

print("%d,%+10.3f" % (s2,pai))  #显示正号,不能显示负号


### 列表元素输出
l = [1,2,3,4]
for i in l:
    print(i,end=" ")  # 以空格为分隔符,不换行,但是最后有空格
    print(i)
 
print(" ".join(str(i) for i in l))  # 以空格为分隔符,最后无空格 

for x in l:
    print(x, end=' ' if x != l[-1] else '')  # 判断该元素是不是列表的最后一个元素,根据判断结果输出分隔符


### format 函数 
### 1.通过槽来填充  
print('hello {0} i am {1}'.format('world','python'))    

print('hello {} i am {}'.format('world','python') ) 

print('hello {0} i am {1} . a new language-- {1}'.format('world','python'))

相关文章

网友评论

      本文标题:python print 用法

      本文链接:https://www.haomeiwen.com/subject/tkbtkltx.html