Python Can';使用彩色字体时,将文本居中
我正在尝试将文本置于终端的中心。我使用:Python Can';使用彩色字体时,将文本居中,python,Python,我正在尝试将文本置于终端的中心。我使用: import shutil from termcolor import colored columns = shutil.get_terminal_size().columns print("This is a "+colored("test",'white','on_red')+" run".center(columns)) 但是,文本没有居中。如果我删除有色部分,它就可以工作。当我尝试将
import shutil
from termcolor import colored
columns = shutil.get_terminal_size().columns
print("This is a "+colored("test",'white','on_red')+" run".center(columns))
但是,文本没有居中。如果我删除
有色部分,它就可以工作。当我尝试将颜色传递到print()函数之外,然后将其传递给asfstring
方法时,它确实可以工作到最后
col = colored("test", 'white', 'on_red')
print(f"This is a {col} run".center(columns))
非常感谢。这确实有效。我想知道为什么。