在Python中,可以使用str.ljust()、str.rjust()和str.center()方法来设置字符串的宽度。
示例代码:
s = "Hello"
width = 10
fillchar = "-"
result = s.ljust(width, fillchar)
print(result) # 输出:Hello-----
示例代码:
s = "Hello"
width = 10
fillchar = "-"
result = s.rjust(width, fillchar)
print(result) # 输出:-----Hello
示例代码:
s = "Hello"
width = 10
fillchar = "-"
result = s.center(width, fillchar)
print(result) # 输出:--Hello---
通过这些方法,可以方便地设置字符串的宽度和对齐方式。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>