python str.format 中文对齐的细节问题
This_is_Y Lv6

参考:
https://www.cnblogs.com/Black-Ice/p/10846848.html
https://blog.csdn.net/code4101/article/details/80985218

解决方法:

1
2
3
4
5
6
7
8
banner = "1.abc"
print('\033[1;31m ***{name:^{len}}*** \033[0m'.format(name=banner,len=22-len(banner.encode('GBK'))+len(banner)))

banner = "2.纯中文"
print('\033[1;31m ***{name:^{len}}*** \033[0m'.format(name=banner,len=22-len(banner.encode('GBK'))+len(banner)))

banner = "3.中文and alphabet"
print('\033[1;31m ***{name:^{len}}*** \033[0m'.format(name=banner,len=22-len(banner.encode('GBK'))+len(banner)))

效果:

image-20220422205340602

 评论
评论插件加载失败
正在加载评论插件
由 Hexo 驱动 & 主题 Keep
访客数 访问量