0

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

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

Help me out of this please

0

2 Answers 2

9

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' % ':' : : 
Sign up to request clarification or add additional context in comments.

3 Comments

But you left off the punchline: the original statement probably needed a "," to prevent this from happening.
@S.Lott: and that would add the soft space :)
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].
2

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.