python 如何工整的打印字符串

# 如何工整的打印字符串 [对字符串进行左中右对齐的操作]

from collections import OrderedDict


my_dict = OrderedDict()
my_dict["name"] = "Hui Zhang"
my_dict["age"] = "21"
my_dict["gender"] = "M"
my_dict["email"] = "[email protected]"
my_dict["school"] = "Hua Zhong University Of Science & Technology"

max_len_key = max(map(len, my_dict.keys()))
max_len_val = max(map(len, my_dict.values()))

# 使用字符串的str.ljust() rjust(), center()
print("Tip1".center(100, "="))
for k, v in my_dict.items():
    print("{}: {}".format(k.ljust(max_len_key, "-"), v.ljust(max_len_val)))

print(format("Tip2", "^100"))
# 使用format函数进行调整
for k, v in my_dict.items():
    print("{}: {}".format(format(str(k), "<10"), format(str(v), ">50")))





你可能感兴趣的:(PYTHON)