views:

126

answers:

2

I am not getting why the colon shifted left in the second time

>>> print '%5s' %':'
    :
>>> print '%5s' %':' '%2s' %':'
 : :

Help me out of this please

+2  A: 

What are you trying to do?

>>> print '%5s' % ':'
    :
>>> print '%5s%2s' % (':', ':')
    : :

You could achieve what you want by mixing them both into a single string formatting expression.

Paolo Bergantino
+8  A: 

In Python, juxtaposed strings are concatenated:

>>> t = 'a' 'bcd'
>>> t
'abcd'

So in your second example, it is equivalent to:

>>> print '%5s' % ':%2s' % ':'

which by the precedence rules for Python's % operator, is:

>>> print ('%5s' % ':%2s') % ':'

or

>>> print ' :%2s' % ':'
 : :
Greg Hewgill
But you left off the punchline: the original statement probably needed a "," to prevent this from happening.
S.Lott
@S.Lott: and that would add the soft space :)
SilentGhost
Or, instead of inserting a comma and adding a soft space you can insert a "+" between the first colon and the "%2s" and get the probably intended result of (in words because I don't expect this to come our correct any other way) [four spaces][colon][one space][colon].
David Locke