Python字符串对齐方法(ljust()、rjust()和center())详解
Python ljust()方法
ljust() 方法的功能是向指定字符串的右侧填充指定字符,从而达到左对齐文本的目的。ljust() 方法的基本格式如下:
S.ljust(width[, fillchar])
其中各个参数的含义如下:
S:表示要进行填充的字符串;
width:表示包括 S 本身长度在内,字符串要占的总长度;
fillchar:作为可选参数,用来指定填充字符串时所用的字符,默认情况使用空格。
【例 1】
S = 'http://c.biancheng网站站点" rel="nofollow" />
http://c.biancheng网站站点" rel="nofollow" />
S = 'http://c.biancheng网站站点" rel="nofollow" />
http://c.biancheng网站站点" rel="nofollow" />
其中各个参数的含义和 ljust() 完全相同,所以这里不再重复描述。S.rjust(width[, fillchar])
【例 3】
S = 'http://c.biancheng网站站点" rel="nofollow" />
http://c.biancheng网站站点" rel="nofollow" />
S = 'http://c.biancheng网站站点" rel="nofollow" />
-----http://c.biancheng网站站点" rel="nofollow" />
其中各个参数的含义和 ljust()、rjust() 方法相同。S.center(width[, fillchar])
【例 5】
S = 'http://c.biancheng网站站点" rel="nofollow" />
http://c.biancheng网站站点" rel="nofollow" />
S = 'http://c.biancheng网站站点" rel="nofollow" />
---http://c.biancheng网站站点" rel="nofollow" />