Reputation: 9133
Say I want to print like this:
print 1,"hello", 2, "fart"
but with tabs instead of spaces, what is the most pythonic way of doing this, in python 2?
It's a really silly question but I couldn't seem to find an answer!
Upvotes: 5
Views: 6527
Reputation: 24278
I would use a generator expression like
print '\t'.join((str(i) for i in (1, "hello", 2, "fart")))
Upvotes: 0
Reputation: 70552
Another approach is to look towards the future!
# Available since Python 2.6
from __future__ import print_function
# Now you can use Python 3's print
print(1, 'hello', 2, 'fart', sep='\t')
Upvotes: 7
Reputation: 8244
Using str.join
:
print '\t'.join(map(str, (1, "hello", 2, "fart")))
Upvotes: 3