蟒紋

2014-04-05 37 views
0

好吧,我問我的問題最後一次是在一個不合時宜的方式,我應該問是怎麼做的我CENTER在屏幕中央提出的問題? 我上次收錄了這個截圖,我想在屏幕的MIDDLE中提問:D。抱歉。 Techhelp.img蟒紋

謝謝!任何幫助深表感謝!

+0

見[此](http://stackoverflow.com/questions/13383244/python-centre-string-using-format-specifier)題畫面寬度檢測,也許這snippet適合你嗎?:http://www.aliendev.com/programming/snippet-friday-programming/quick-snippet-friday-center-text-in-a-console-python-program – Linuxios

回答

1

使用從here

def center_print(s): 
    import os,sys 
    command = "mode con^| findstr Columns" if sys.platform == 'win32' else "stty size" 
    ignore, columns = os.popen(command).read().split() 
    print " "*((int(columns)-len(s))/2)+s 

center_print("aaaaaaaaaaaaaaaa") 
1

您可以定義一個特殊打印功能是這樣的:

terminal_width = 80 # Change this if necessary. 

def center_print(s): 
    print '{:^terminal_width}'.format(s) 

,並調用它,當你在中心需要的輸出。因此,請致電該功能而不是print以解決您的問題。

+0

要清楚:這個中心消息在屏幕的_expected_寬度中,不需要_actual_寬度。 –