Heim  >  Artikel  >  Backend-Entwicklung  >  在Python中处理字符串之ljust()方法的使用简介

在Python中处理字符串之ljust()方法的使用简介

WBOY
WBOYOriginal
2016-06-10 15:12:121921Durchsuche

 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, '0');

当我们运行上面的程序,它会产生以下结果:

this is string example....wow!!!000000000000000000

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn