栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python

str.rjust|ljust|center(width[, fillchar])

1、 rjust() 返回一个原字符串右对齐,ljust() 返回一个原字符串左对齐,center() 返回一个原字符串居中;

a = 'hello world'
def get_str(strs,width,fillchar=' '):
    global a
    print(eval("a.{}({},'{}')".format(strs,width,fillchar)))

get_str('rjust', 22)
get_str('ljust', 22, '*')
get_str('center', 22, '*')

 out:
               hello world
hello world***********
*****hello world******

2、默认填充字符为空格填充至长度 width 的新字符串,如果指定的长度小于原字符串的长度则返回原字符串。

get_str('rjust', 10)
get_str('ljust', 10, '*')
get_str('center', 10, '*')

 out:
hello world
hello world
hello world

3、当center()无法使左右字符数相等时候,字符串字符数为偶数时左侧字符会比右侧多 1,字符串字符数为奇数时左侧字符会比右侧少 1

get_str('center', 14, '*')
a = 'helloworld'
get_str('center', 13, '*')

 out:
*hello world**
**helloworld*

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/741521.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号