Python基础(22) - 如何让字符串居中显示
2020-03-02 本文已影响0人
xianling_he
如何让字符串居中显示,有哪些方法 可以使用center & Format 方法
- 使用字符串center方法
print('<' + 'hello'.center(30) + '>')

- 使用Format方法
print('<{:^30}>'.format('hello'))

使用center,format方法让字符串居中显示,两侧显示井号(#)
- 使用'hello'.center(30,'') 表示用星号()分隔,总字符串的长度是30
print('<' + 'hello'.center(30,'*') + '>')

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

总结
使用字符串的center方法及format方法可以让字符串居中显示,功能相同
加油 2020-3-2