Python

Python基础(22) - 如何让字符串居中显示

2020-03-02  本文已影响0人  xianling_he

如何让字符串居中显示,有哪些方法 可以使用center & Format 方法

print('<' + 'hello'.center(30) + '>')
hexianling.png
print('<{:^30}>'.format('hello'))
hexianling.png

使用center,format方法让字符串居中显示,两侧显示井号(#)

  1. 使用'hello'.center(30,'') 表示用星号()分隔,总字符串的长度是30
print('<' + 'hello'.center(30,'*') + '>')
hexianling.png
  1. 使用'<{:#^30}>'.format('hello') 表示用井号(#)分隔,总字符串的长度是30
print('<{:#^30}>'.format('hello'))
hexianling.png

总结

使用字符串的center方法及format方法可以让字符串居中显示,功能相同

加油 2020-3-2

上一篇下一篇

猜你喜欢

热点阅读