3

How can i keep white space in a string?

I have a string

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

**
 *
 *

and I want print this in the center, but it I use this function, doesn't keep the white space

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

**
*
 *

I had this output

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

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

         **         
         *          
          *    

as you can see the second move is right but when I print it on the center something is wrong

fege
  • 547
  • 3
  • 7
  • 19
  • Can you wrap the string in quotations so we can see exactly what the string is? – joshreesjones May 18 '13 at 19:04
  • what is piece set to? – dansalmo May 18 '13 at 19:06
  • piece is "**\n *\n *" – fege May 18 '13 at 19:09
  • I am getting different output than yours : http://ideone.com/R3lW6l – Ashwini Chaudhary May 18 '13 at 19:16
  • For me, it works. The blanks and stars are not printed at the biginning of lines, but in the middle of a 20 chars span on the display - Are you sure you obtain a blank at beginning of the third line before *, and no blank before * on the second line ? – eyquem May 18 '13 at 19:17
  • 1
    What does mean _"insert a move w"_ ?? Do you realize that we understand nothing to what you write ? – eyquem May 18 '13 at 19:34
  • move = raw_input("insert a move ") and just change the figure(string) the problem is after piece before centring print something after what I apply the function the print is different – fege May 18 '13 at 19:48

1 Answers1

2

in python 3 i am getting this :

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