![Trending Articles on Technical and Non Technical topics](/images/trending_categories.jpeg)
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to print a complete tuple in Python using string formatting?
When using the old style of string formatting in python, ie, "" % (), if the thing after the percent is a tuple, python tries to break it down and pass individual items in it to the string. For example,
tup = (1,2,3) print("this is a tuple %s" % (tup))
This will give the output:
TypeError: not all arguments converted during string formatting
This is because of the reason mentioned above. If you want to pass a tuple, you need to create a wrapping tuple using the (tup, ) syntax. For example,
tup = (1,2,3) print("this is a tuple %s" % (tup, ))
This will give the output:
this is a tuple (1, 2, 3)
The (tup,) notation differentiates a single-valued tuple from an expression.
Advertisements