Python中字符串对齐的方法是什么

   2024-10-13 5250
核心提示:在Python中,可以使用字符串的ljust(),rjust()和center()方法来实现字符串的左对齐、右对齐和居中对齐。# 左对齐s = helloleft_

在Python中,可以使用字符串的ljust()rjust()center()方法来实现字符串的左对齐、右对齐和居中对齐。

# 左对齐s = "hello"left_aligned = s.ljust(10)print(left_aligned)# 右对齐right_aligned = s.rjust(10)print(right_aligned)# 居中对齐center_aligned = s.center(10)print(center_aligned)

在上面的示例中,ljust()方法将字符串左对齐,并在右侧填充空格以使字符串总长度为10;rjust()方法将字符串右对齐,并在左侧填充空格;center()方法将字符串居中对齐,并在两侧填充空格。

 
举报打赏
 
更多>同类维修大全
推荐图文
推荐维修大全
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号