美文网首页Python
Python基础(22) - 如何让字符串居中显示

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

作者: xianling_he | 来源:发表于2020-03-02 14:44 被阅读0次

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

    • 使用字符串center方法
    print('<' + 'hello'.center(30) + '>')
    
    hexianling.png
    • 使用Format方法
    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

    相关文章

      网友评论

        本文标题:Python基础(22) - 如何让字符串居中显示

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