字符串对齐
来源:互联网 发布:师洋淘宝店截图 编辑:程序博客网 时间:2024/05/16 18:05
字符串对齐工作是我们经常碰到的,在python cookbook中有那么一小节有讲述,总结如下:
@python>>>text='hello world'>>>text.rjust(20)' hello world'>>>text.ljust(20)'hello world '>>>text.center(20)' hello world '#上面三个例子就是ljust,rjust,center的对齐工作了#当然,还有厉害点的>>>text.rjust(30,'*')'*******************hello world'>>>text.ljust(30,'=')'hello world==================='>>>text.center(30,'*')'*********hello world**********'#其实对于上述三个字符串对齐方法,都可以用一个format方法来解决的.看自己喜好:>>>format(text,'>20')#右对齐' hello world'>>>format(text,'<20')#左对齐'hello world '>>>format(text,'^20')#居中' hello world '#用format当然也是可以替换空白符的了>>>format(text,'*>20')#只需要将你想要的字符放在缩进方向前面即可'*********hello world'>>>format(text,'=<20')'hello world========='>>>format(text,'*^20')'****hello world*****'#Note that:替换字符只能单个字符,比如:>>>format(text,'*=^20')Traceback (most recent call last): File "<stdin>", line 1, in <module>ValueError: Invalid conversion specification#format的功能远不止这些,更多的希望大家可以自己探索
0 0
- 字符串对齐
- 字符串对齐
- java字符串对齐方法
- Python - 字符串对齐
- C# 字符串对齐
- python数字、字符串对齐
- 字符串对齐的方法
- 字符串左对齐
- 字符串 格式对齐
- 字符串输出对齐问题
- 字符串数组对齐问题
- 实现字符串的对齐(左对齐、右对齐、居中)
- 扩展字符串左右对齐方法
- 扩展字符串左右对齐方法
- python coobook 2 ---字符串对齐
- 001_004 Python 字符串对齐
- java字符串应用之字符串对齐器
- 从字符串编辑距离到字符串对齐
- 《设计模式》-- 装饰模式
- 06WEBBASIC_07jQuery
- 进程和线程
- 猴子选大王
- 排序算法系列二之快速排序
- 字符串对齐
- golang实现简单文件服务器
- 基于Netty5.0中级案例一之Netty与Web
- 08Servlet
- 凤凰网实习生面试
- C++函数指针
- JVM知识点
- Codeforces Round #403 (Div. 2) B. The Meeting Place Cannot Be Changed 三分
- Java程序员必须知道的两个有关Eclipse设置