2013-05-18 289 views
2

如何在字符串中保留空格?Python在字符串中保留空格

我有一個字符串

piece = "**\n *\n *" 

** 
* 
* 

,我想在中心打印這一點,但我用這個功能,不留空白

print '\n'.join('{0:^20}'.format(x, 'centered') for x in piece.split('\n')) 

** 
* 
* 

我有這個輸出

 *   
     *   
     **   
insert a move w 
piece before centring 
    * 
*** 

      *   
     ***   
insert a move w 
piece before centring 
** 
* 
* 

     **   
     *   
      *  

正如你所看到的第二步是正確的,但是當我在中心打印它是錯誤的

+0

你能包裝在報價的字符串,所以我們可以看到字符串到底是什麼? – joshreesjones

+0

什麼是作品集? – dansalmo

+0

件是「** \ n * \ n *」 – fege

回答

1

在Python 3我得到這個:

>>> print ('\n'.join('{0:^20}'.format(x, 'centered') for x in piece.split('\n')) 
) 
     ** 
      * 
      * 
>>> 
+0

你正在嘗試什麼python版本? –

+0

的版本是2.7 – fege

+0

與linux上的python 2.7.5相同 – wikier

相關問題