• 技术文章 >Python技术 >Python基础教程

    python填充压缩的函数总结

    小妮浅浅小妮浅浅2021-09-24 09:40:51原创3614

    1、ljust、rjust字符串从左/右开始,不够就在后/前填充。

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    # a.ljust()、a.rjust # 字符串从左边/右边开始,不够在后面/前面填充

    s = 'girl'

    s_new = s.ljust(30,'-')  #填充字符的长度为1

    s_new1 = s.rjust(30,'-')

    print(s_new)

    print(s_new1)

      

    输出:

    girl--------------------------

    --------------------------girl

    2、center字符串两侧填充。

    1

    2

    3

    4

    5

    6

    7

    # a.center()  # 字符串按照指定长度两边填充

    s = 'girl'

    s_new = s.center(50,'-')

    print(s_new)

      

    输出:

    -----------------------girl-----------------------

    3、ltrip 左边空格、\n、tab键等都去掉。

    1

    2

    3

    4

    5

    6

    7

    8

    # a.ltrip() # 左边空格、\n、tab键等都去掉

    s = '  \ngood day \n  \thello'

    s_new = s.lstrip()

    print(s_new)

      

    输出:

    good day

       hello

    4、rtrip右边空格、\n、tab键等都去掉。

    1

    2

    3

    4

    5

    6

    7

    # a.rtrip() # 右边空格、\n、tab键等都去掉

    s = '  good day \n  \t'

    s_new = s.rstrip()

    print(s_new)

      

    输出:

      good day

    以上就是python填充压缩的函数总结,希望对大家有所帮助。更多Python学习指路:python基础教程

    本文教程操作环境:windows7系统、Python 3.9.1,DELL G3电脑。

    专题推荐:python填充 函数
    上一篇:python查找计算函数的整理 下一篇:python分割拼接函数的介绍

    相关文章推荐

    • python异常处理的流程• python中__new__的使用注意• python索引的顺序和倒序• python循环遍历如何理解• python for语句的应用场景• python如何模拟用户自动打卡• python逻辑取反的实现• python Pytest有什么特点• python如何打印矩阵• Python Modules是什么意思• python三种导入模块的方式• python查找计算函数的整理

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网