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

    python中如何用ljust()实现字符串左对齐?

    宋雪维宋雪维2021-02-20 09:23:54原创2991

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

    1、ljust()方法

    字符串的排版方法。

    ljust()方法将原字符串左对齐,使用空格填充至指定长度,并返回新的字符串。

    如果指定的长度小于或等于原字符串长度,则直接将字符串返回。

    2、语法

    str.ljust(width[, fillchar])
    
    .ljust(width[, fillchar])

    3、参数

    width :指定字符串长度;

    fillchar :填充字符,默认为空格。

    4、返回值

    ljust方法返回一个左对齐排版的字符串

    5、实例

    # -*- coding: utf-8 -*- # @File: check_data.py # @Project: test_project # @Date: 2019/3/28 9:25 # @Author: MaiXiaochai def ljust_demo(): string = "Hello Python!" print("string_len: ", len(string)) print(string.ljust(15, '+'))

    输出

    # Out:
    
    string_len:  13
    
    Hello Python!++

    以上python中用ljust()实现字符串左对齐的方法,希望能对你有所帮助哦~

    专题推荐:pythonjust
    品易云
    上一篇:如何使用python的callable函数? 下一篇:python中dir函数如何使用?

    相关文章推荐

    • python中reshape函数怎么用?• python concat函数有何用法?• 如何使用python的callable函数?• python中如何读取excel某列数据?• 如何使用Python脚本完成视频播放?

    全部评论我要评论

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

  • 取消发布评论
  • 

    Python学习网