eye_mew
eye_mew

Reputation: 9133

Printing with tab separation instead of spaces

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

Answers (3)

David Zwicker
David Zwicker

Reputation: 24278

I would use a generator expression like

print '\t'.join((str(i) for i in (1, "hello", 2, "fart")))

Upvotes: 0

voithos
voithos

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

Markus Unterwaditzer
Markus Unterwaditzer

Reputation: 8244

Using str.join:

print '\t'.join(map(str, (1, "hello", 2, "fart")))

Upvotes: 3

Related Questions