首頁  >  文章  >  後端開發  >  Python中字符串对齐方法介绍

Python中字符串对齐方法介绍

WBOY
WBOY原創
2016-06-10 15:12:042004瀏覽

目的

  实现字符串的左对齐,右对齐,居中对齐。

方法

  字符串内置了以下方法:其中width是指包含字符串S在内的宽度,fillchar默认是空格,也可以指定填充字符

复制代码 代码如下:

string.ljust(s, width[, fillchar])
string.rjust(s, width[, fillchar])
string.center(s, width[, fillchar])

复制代码 代码如下:

In [6]: a='Hello!'

In [7]: print a.ljust(10,'+')
Hello!++++

In [8]: print a.rjust(10,'+')
++++Hello!

In [9]: print a.center(10,'+')
++Hello!++

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn