Python keep whitespace in a string

2.8k Views Asked by At

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

1

There are 1 best solutions below

2
On

in python 3 i am getting this :

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