2

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.

1
  • SOmetimes when writing quick-and-dirty scripts I have been known to use .format(**locals()) (so I can refer to local variables inside the string). Commented Nov 21, 2011 at 11:41

2 Answers 2

4

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

4 Comments

I didn't know you could use aliases like that. Nice one.
@rplnt: Giving links without annotations is not helpful, because you do not know whether link is useful to you until you click on it. But in fact, that link provides more examples of how to use .format() method of str objects.
@Tadeck I thought that the link was pretty self-explanatory (format-examples). But I'll be more careful in the future, thanks.
@rplnt: Thanks. You can add explanations to links in the following way: [some explanation about Example.com](http://www.example.com/). Good luck.
0

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

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

1 Comment

This, in fact, gives me an idea. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.