Python 如何居中打印

Python 如何居中打印,python,string,formatting,Python,String,Formatting,我有一个字符串,如下所示 string = "I went to the market the other day" vowels = "AEIOUaeiou" vcount = 0 for letter in string: vcount+=1 print ("Vowels:", vcount) 假设您说的是在屏幕上居中,我如何使用string.center将打印的数据居中: 标准输出是一个流,没有“屏幕”的概念,没有“宽度”,因此不能居中 使用软件包 或者,如果你假设一个固定的大

我有一个字符串,如下所示

string = "I went to the market the other day"
vowels = "AEIOUaeiou"
vcount = 0

for letter in string:
    vcount+=1
print ("Vowels:", vcount)

假设您说的是在屏幕上居中,我如何使用string.center将打印的数据居中:

标准输出是一个流,没有“屏幕”的概念,没有“宽度”,因此不能居中

使用软件包

或者,如果你假设一个固定的大小(例如:80个字符),这是一个简单的数学计算。您需要(80长度)/2个空格作为前缀。

您可以使用该功能在console center中打印:

# Where 80 is default width of console in characters
print("Vowels: %s".center(80) % vcount)

中心在哪里?与什么相关?当您知道行的大小时,您可以执行
打印(content.center(line_size))
。这就是你的意思吗?但是,如果你想处理这些情况,你应该考虑下面的Kaloy建议。或者使用字符串格式化:<代码>计数:{:^ 80 }。格式化(V计数)< /Cord>我所要完成的是中心值,所有的值应该看起来像元音:当中心时的空间3。