python字符串填充(转)
2024-08-25 19:24:02
ljust()方法返回字符串左对齐的字符串长度宽度。填充是通过使用指定的fillchar(默认为空格)。如果宽度小于len(s)返回原始字符串。
语法
以下是ljust()方法的语法:
str.ljust(width[, fillchar])
参数
- width -- 这是填充后字符串的总长度。
- fillchar -- 这是填充符,默认为空格。
返回值
此方法返回新字符串。填充是通过使用指定fillchar(默认为空格)。如果宽度小于len(s),返回原始字符串。
例子
下面的例子显示了ljust()方法的使用。
#!/usr/bin/python
str = "this is string example....wow!!!";
print str.ljust(50, '');
当我们运行上面的程序,它会产生以下结果:
this is string example....wow!!!000000000000000000
同理,这些ljust、rjust、center方法都可以用
如:
>>> a="Hello world"
>>> print a.rjust(20)
' Hello world'
>>> print a.ljust(20)
'Hello world '
>>> print a.center(20)
' Hello world '
>>> print a.rjust(20,'*')
'*********Hello world'
>>> print a.ljust(20,'*')
'Hello world*********'
>>> print a.center(20,'*')
'****Hello world*****'
最新文章
- JS各种方法
- 国内及Github优秀开发人员列表
- 一步一步教你安装openstack
- Shell命令_for
- linux tcp协议定时器
- 控制反转和spring在项目中可以带来的好处
- BestCoder Round #70 Jam's math problem(hdu 5615)
- Fling!
- AC自动机跟随Kuangbing学习笔记
- hdoj 1532 Drainage Ditches(最大网络流)
- 用c#实现单链表(程序代码已经验证,完全正确)
- protocol error, got 'n' as reply type byte
- REST架构概述
- java面向对象——类
- asp.net core NLog将日志写到文件
- js间隔一段时间打印数据库中的值
- linux中fork()函数详解【转】
- [转]mysql大表更新sql的优化策略
- pycharm tips
- zabbix监控指定端口