扫描二维码下载沐宇APP

沐宇

微信扫码使用沐宇小程序

沐宇

Python怎么将字符串按照指定宽度居中对齐

扬州沐宇科技
2024-03-02 13:26:26
python

可以使用字符串的center()方法来将字符串按照指定宽度居中对齐。示例如下:

s = "hello"
width = 10
centered_string = s.center(width)
print(centered_string)

运行结果为:

  hello   

center()方法中,传入一个整数参数表示要居中对齐的宽度,该方法会在字符串两侧填充空格,使得字符串居中显示。

扫码添加客服微信