spitfiredd
spitfiredd

Reputation: 3135

Printing a tuple

I have a tuple m = ('ring', 5) and I want to print it out as ring 5.

I have tried print %s %f, %m and I get an error. What am I doing wrong?

Upvotes: 1

Views: 157

Answers (4)

Xuan
Xuan

Reputation: 5629

Use format it is more elegant

print '{0} {1}'.format(*m)

https://stackoverflow.com/a/5082482/243431

Upvotes: 1

Sukrit Kalra
Sukrit Kalra

Reputation: 34531

>>> m = ('ring', 5)
>>> for element in m:
        print element,
ring 5

This might work.

Upvotes: 0

jamylak
jamylak

Reputation: 133764

>>> m = ('ring', 5)

what you were trying to do

>>> print "%s %f" % m
ring 5.000000

Except %f means float and you want %d for int:

>>> print "%s %d" % m
ring 5

Upvotes: 6

Michel Keijzers
Michel Keijzers

Reputation: 15377

m = ('ring', 5)
print m[0], m[1]

prints

ring 5

Upvotes: 0

Related Questions