Files
2025-09-10 16:12:45 +08:00

23 lines
931 B
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

if __name__ == "__main__":
text = 'Hello World'
# 如果想要对齐字符串可以使用ljest、rjust和center方法
print(text.ljust(20))
print(text.rjust(20))
print(text.center(20))
# 当然,这个轮椅也支持填充操作
print(text.ljust(20, '='))
print(text.rjust(20, '-'))
print(text.center(20, '='))
# 除了轮椅函数建议使用format函数这个东西更加泛用还能进行格式转换
print(format(text, '-<20')) # <表示宽度20居左
print(format(text, '=>20')) # >表示宽度20居右
print(format(text, '+^20')) # ^表示宽度20居中
# <>^前面的符号表示使用该符号填充
# format的好处主要在于它的处理对象不仅是字符串可以对任何数据形式进行格式化
num = 1.23456
print(format(num, '=^20.2f')) # 这表示宽度20保留两位小数居中使用=填充