在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()方法将字符串居中对齐,并在两侧填充空格。


