兩學一做材料上哪個網站找最佳的搜索引擎
一、ljust()
語法:str.ljust(width,[fillchar])
參數(shù)說明:
width – 指定字符串長度。
fillchar – 填充字符,默認為空格。
返回值:返回一個原字符串左對齊,并使用空格填充至長度 width 的新字符串。如果指定的長度小于原字符串的長度則返回原字符串。
str = "this is string example....wow!!!"
print(str.ljust(50, '0'))
# this is string example....wow!!!00000000
二、rjust()
語法:str.rjust(width,[fillchar])
參數(shù)說明:
width – 指定字符串長度。
fillchar – 填充字符,默認為空格。
返回值:返回一個原字符串右對齊,并使用空格填充至長度 width 的新字符串。如果指定的長度小于原字符串的長度則返回原字符串。
str = "this is string example....wow!!!"
print(str.rjust(40, '0'))
# 00000000this is string example....wow!!!
三、zfill(width)
語法:str.zfill(width)
width – 指定字符串的長度。原字符串右對齊,前面填充0。
返回長度為 width 的字符串,原字符串 string 右對齊,前面填充0
str = "this is string example....wow!!!"
print(str.zfill(40))
# 00000000this is string example....wow!!!
print(str.zfill(50))
# 000000000000000000this is string example....wow!!!
四、string.center(width)
語法:str.center(width, [fillchar])
width – 字符串的總寬度。fillchar – 填充字符。
返回一個原字符串居中,并使用空格填充至長度width的新字符串
(如果不指定填充字符,默認填充符為空格)
str = 'runoob'
print(str.center(20, '*'))
# '*******runoob*******'
print(str.center(20))
# ' runoob '