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*****'

最新文章

  1. JS各种方法
  2. 国内及Github优秀开发人员列表
  3. 一步一步教你安装openstack
  4. Shell命令_for
  5. linux tcp协议定时器
  6. 控制反转和spring在项目中可以带来的好处
  7. BestCoder Round #70 Jam's math problem(hdu 5615)
  8. Fling!
  9. AC自动机跟随Kuangbing学习笔记
  10. hdoj 1532 Drainage Ditches(最大网络流)
  11. 用c#实现单链表(程序代码已经验证,完全正确)
  12. protocol error, got 'n' as reply type byte
  13. REST架构概述
  14. java面向对象——类
  15. asp.net core NLog将日志写到文件
  16. js间隔一段时间打印数据库中的值
  17. linux中fork()函数详解【转】
  18. [转]mysql大表更新sql的优化策略
  19. pycharm tips
  20. zabbix监控指定端口

热门文章

  1. 查看端口 (windows)
  2. 分析报告:云之家V9 VS 钉钉3.5
  3. 中点Brehensam画圆算法
  4. 学习笔记之FluentAssertions
  5. linux 守护进程 daemon
  6. HttpURLConnection连接超时问题
  7. 6.15-初识JSP、javaweb
  8. 使用eclipse在linux下开发C/C++
  9. Where is virtualenvwrapper.sh after pip install?
  10. 15. Studio上字符串转整形、整形转字符串例子