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.
2 Answers 2
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
answered Nov 21, 2011 at 11:15
Tadeck
138k28 gold badges156 silver badges201 bronze badges
Sign up to request clarification or add additional context in comments.
4 Comments
dierre
I didn't know you could use aliases like that. Nice one.
Tadeck
@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.rplnt
@Tadeck I thought that the link was pretty self-explanatory (format-examples). But I'll be more careful in the future, thanks.
Tadeck
@rplnt: Thanks. You can add explanations to links in the following way:
[some explanation about Example.com](http://www.example.com/). Good luck.You can do something like this but it's quite ugly.
column = 3
message = '{'+str(column)+'}'
print message.format(0,0,0,'spam')
answered Nov 21, 2011 at 11:21
dierre
7,22012 gold badges80 silver badges123 bronze badges
1 Comment
Academia
This, in fact, gives me an idea. Thanks a lot.
lang-py
.format(**locals())(so I can refer to local variables inside the string).