
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Complete Tuple in Python Using String Formatting
A tuple in Python is an ordered collection of items that cannot be changed once, making it immutable. This allows duplicate values and can hold elements of different data types, such as strings, numbers, other tuples, and more. This makes tuples useful for grouping related data while determining that the content remains fixed and unchanged.
How to Display an Entire Tuple in Python?
To display a complete tuple in Python, we can simply use the print() function. Here's a basic example -
tuple = (3, 4, 5, 6, 7, 1, 2) print(tuple)
The result is obtained as follows -
(3, 4, 5, 6, 7, 1, 2)
If we are handling a large tuple and want to make sure this doesn't get truncated, we can convert it to a string through formatting. Following example -
tuple = (3, 4, 5, 6, 7, 1, 2) print(tuple) print(str(tuple)) tuple = ("grapes", "berry", "watermelon") print(", ".join(tuple))
The output is generated as follows -
(3, 4, 5, 6, 7, 1, 2) (3, 4, 5, 6, 7, 1, 2) grapes, berry, watermelon
Formatting a String to Display a Complete Tuple
In Python, if we want to print a complete tuple using string formatting, we can use any of the formatting methods like -
-
Using f-string
-
Using str.format()
-
Using % formatting
Using f-string
In Python, an f-string allows the expression inside a string that is constantly using curly braces "{}". They start with f and F before the opening quote. The f-string contains a readable way to specify variables or expressions in strings. These are faster than the other formatting methods.
Example
A tuple is defined using an f-string (f"..."), and the tuple is formatted into a string for display.
tuple = (4, 5, 6, 7, 8) print(f"Tuple is: {tuple}")
We will get the output as follows -
Tuple is: (4, 5, 6, 7, 8)
Using str.format()
The str.format() in Python is used to insert a variable into strings. Placeholders{} are used in the string, and format() replaces them with values. We can also use positional or named arguments, as this is readable and versatile.
Example
Here, we are defining a tuple(4, 5, 6, 7), and the formatting method is used to insert the tuple into the string.
tuple = (4, 5, 6, 7) print("Tuple is: {}".format(tuple))
The output is as follows -
Tuple is: (4, 5, 6, 7)
Using % formatting
In Python, % formatting uses formats like %s to insert values into strings. While printing a tuple, we include a comma to determine this as a single argument rather than multiple values.
tuple = (4, 5, 6) print("Tuple is: %s" % (tuple,))
The result is generated as follows -
Tuple is: (4, 5, 6)
Example
When using the old style of string formatting in Python(i.e., " ",%, ()), if the object follows the percent symbol and is a tuple, Python specifies it and passes its individual elements to the string.
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 occurs due to the above-mentioned reason. To pass a tuple, we can create a tuple using (tup, ) -
tup = (1,2,3) print("this is a tuple %s" % (tup, ))
This will give the following output -
this is a tuple (1, 2, 3)
The (tup,) notation differentiates a single-valued tuple from an expression.