推扬网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
推扬网 门户 经验分享 查看内容

Python中字符串对齐方法介绍

2020-4-11 13:41| 发布者: admin| 查看: 301| 评论: 0

这篇文章主要介绍了Python中字符串对齐方法介绍,本文介绍Python字符串内置方法ljust、rjust、center的用法,需要的朋友可以参考下

目的

  实现字符串的左对齐,右对齐,居中对齐。

方法

  字符串内置了以下方法:其中width是指包含字符串S在内的宽度,fillchar默认是空格,也可以指定填充字符

string.ljust(s, width[, fillchar])
string.rjust(s, width[, fillchar])
string.center(s, width[, fillchar])


In [6]: a='Hello!'

In [7]: print a.ljust(10,'+')
Hello!++++

In [8]: print a.rjust(10,'+')
++++Hello!

In [9]: print a.center(10,'+')
++Hello!++


鲜花

握手

雷人

路过

鸡蛋

最新评论

精选推荐

    广告服务|投稿要求|禁言标准|版权说明|免责声明|手机版|小黑屋|推扬网 ( 粤ICP备18134897号 )|网站地图 | 邮箱:vayae@hotmail.com

    GMT+8, 2024-3-28 18:07 , Processed in 0.341749 second(s), 28 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    返回顶部