Academia
Academia

Reputation: 4124

print formatting in python

I have a question: How can I print a character on a specific stdout column?

I know that:

print '{0} and {1}'.format('spam', 'eggs')

prints spam on the first column and eggs on the second one.

But I want to do this:

column = 3
...
print '{column}'.format('spam')

cheers.

Upvotes: 2

Views: 351

Answers (2)

dierre
dierre

Reputation: 7220

You can do something like this but it's quite ugly.

column = 3
message = '{'+str(column)+'}'
print message.format(0,0,0,'spam')

Upvotes: 0

Tadeck
Tadeck

Reputation: 137450

You have two options to do it.

First option - pass it in parameter:

>>> print '{column}'.format(column='spam')
spam

Second option - unpack a dictionary (using **):

>>> print '{column}'.format(**{'column':'spam'})
spam

Upvotes: 4

Related Questions