By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,199 Members | 1,125 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,199 IT Pros & Developers. It's quick & easy.

tab in a print statement in Python

P: 1
I'm trying to print two variables together in one print statement in Python and getting TypeError: __add_nor_radd__ defined for these operands.

Any one has idea whats going on with this print statement?

Here is what i'm doing:

print var1+var2+var3 ---> all 3 var's are of integer type and when I execute this print statement prints only var1

print var1 +'\t'+ var2 ---> gives the above error mentioned above.
Sep 18 '11 #1
Share this Question
Share on Google+
1 Reply


bvdet
Expert Mod 2.5K+
P: 2,851
Use string formatting to print variables mixed with strings:
Expand|Select|Wrap|Line Numbers
  1. >>> var1 = 1
  2. >>> var2 = 2
  3. >>> var3 = 3
  4. >>> print var1+var2+var3
  5. 6
  6. >>> print var1+'\t'+var3
  7. Traceback (most recent call last):
  8.   File "<interactive input>", line 1, in ?
  9. TypeError: unsupported operand type(s) for +: 'int' and 'str'
  10. >>> print "%s\t%s" % (var1, var3)
  11. 1    3
  12. >>> print "%s %s %s" % (var1, var2, var3)
  13. 1 2 3
  14. >>> 
Sep 18 '11 #2

Post your reply

Sign in to post your reply or Sign up for a free account.