python中字符串的ljust、rjust、center方法讲解
2016-04-20 21:25
609 查看
python中字符串的ljust、rjust、center方法讲解
这三种方法的用法差不多:S.ljust(width[, fillchar]),即长度加占位符,默认为空格,这三种在格式化输出时用着非常方便。
如:
>>> 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*****'
这三种方法的用法差不多:S.ljust(width[, fillchar]),即长度加占位符,默认为空格,这三种在格式化输出时用着非常方便。
如:
>>> 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*****'
相关文章推荐
- 关于python文件操作
- 【python】好书
- python语法基础归纳(3)--函数
- python读取邮箱邮件数据并下载附件
- 用ConfigParser模块读写配置文件——Python
- python核心编程第二版 第8章课后题
- [Python]使用Decorator设计Cache
- 解决selenium报错Element is not clickable at point...
- 小白python进击第二周2.尾递归
- python中函数的总结之三
- python中函数的总结之三
- python中函数的总结之三
- sublime中使用python3(windows环境)
- python中的转义字符
- 用Python 编写的清除U8系统单据锁定的脚本
- python中input与raw_input()
- Python @classmethod和@staticmethod区别
- Python supervisor
- Python yield
- Python 类成员函数第一个参数self