print对齐方式

对齐方式的取值:

  • <:左对齐
  • >:右对齐
  • ^:居中
  • =:在正负号(如果有的话)和数字之间填充,该对齐选项仅对数字类型有效。它可以输出类似 +0000120 这样的字符串。
>>> print("|",format("RUNOOB","*>30"),"|")    #左对齐
| ************************RUNOOB |
>>> print("|",format("RUNOOB","*^30"),"|")    #居中对齐
| ************RUNOOB************ |
>>> print("|",format("RUNOOB","*<30"),"|")    #右对齐
| RUNOOB************************ |
>>> 

print("执行开始".center(scale//2,"-"))  # .center() 控制输出的样式,宽度为 25//2,即 22,汉字居中,两侧填充 -
>>> print("执行开始".center(50//2,"-"))
-----------执行开始----------



>>> '{1:3d}'.format(1, 1) #{1:3d}取tuple的index 1,宽度3
'  1'
>>> 
>>> '{1:3d}'.format(1, 12)
' 12'
>>> 
>>> '{1:3d}'.format(1, 123)
'123'
>>> '{1:3d}'.format(1, 12345)
'12345'

>>> '{:3d}'.format(1, 12)  #默认为index 0
'  1'
>>> 

>>> '{1:4d}'.format(12345)   # tuple (12345) index只有0,没有1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> 
>>> '{:^3.0f}'.format(3.1415) #占3位,0个小数点
' 3 '
>>> '{:^3.0f}'.format(24.1415)
'24 '
>>> '{:3.0f}'.format(24.1415)  
' 24'
>>> '{0:3.0f}'.format(24.1415)
' 24'
>>> '{1:3.0f}'.format(24.1415)   #tuple (24.1415) index只有0,没有1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> 
>>> '{:^20.1f}'.format(3.8,)
'        3.8         '
>>>
>>> '{1:3.0f}'.format(1, 12345.1314)
'12345'

>>> '{1:3.8f}'.format(1, 12345.1314)
'12345.13140000'
>>> 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值