我有一個字符串類似如下如何中心蟒紋
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
可以使用str.center
功能在控制檯中心打印:
# Where 80 is default width of console in characters
print("Vowels: %s".center(80) % vcount)
或者使用字符串格式:''Count:{:^ 80}'。format(vcount)' –
我試圖完成的是將所有值坐在中間應該看起來像元音:空間3時居中 – don
假設你打印的數據是在談論屏幕上的中心:
標準輸出是一個流,沒有「屏幕」的概念,沒有「寬度」,因此你不能居中。使用curses軟件包。
另外,如果你假設一個固定的大小(例如:80個字符),這是一個簡單的數學計算。您需要(80長度)/ 2個空格作爲前綴。
中心在哪裏?相對於什麼? – Robin
當你知道行的大小時,可以執行print(content.center(line_size))。你是這個意思嗎?然而,處理大小調整等操作並不是微不足道的。如果你想處理這些情況,你應該考慮遵循Karoly的建議。 – Bakuriu