Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Can';使用彩色字体时,将文本居中_Python - Fatal编程技术网

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()函数之外,然后将其传递给as
fstring
方法时,它确实可以工作到最后

col = colored("test", 'white', 'on_red')
print(f"This is a {col} run".center(columns))

非常感谢。这确实有效。我想知道为什么。