Python Program to print elements of a tuple


In Python, tuples are an important data type for storing a collection of items. Sometimes, it may be necessary to print the elements of a tuple in order to understand or debug your code. In this article, we will be discussing how to print the elements of a tuple in Python.

We will go over the syntax for accessing and printing tuple elements and will provide examples of how to do so. We can define a tuple in the following way −

tup1 = ("this" , "is" , “a” , "tuple")
tup2 = (1, 2, 3, 4, 5 );
tup3 = "a", "b", "c", "d";

In this article, we will see 3 different methods on “how we can print element of a tuple in python.”

Using print()function

In this method, we will use the print function of python to print the whole tuple. The print function takes an argument and print it to the sys.stdout.

Example

In this example, we will print the elements of a tuple using the print function of python, we will first create a tuple and then use the print function to print it to the screen.

tup = ('this' , 'is' , 'a' , 'sample' , 'tuple' )
print( "Your tuple is: ", tup )

Output

The output standard deviation obtained is as follows −

Your tuple is: ( ‘this’ , ‘is’ , ‘a’ , ‘sample’ , ‘tuple’ )

Using indexing

In this method, we will make use of indexing to access the elements of tuple and then print them one by one on the screen.

Example

In this example, we will make a loop for a length of the tuple given and then we will print the element of the tuple at each index.

tup = ( 'this' , 'is' , 'a' , 'sample' , 'tuple' )
print ("your tuple is: ")
for i in range ( len(tup) ):
   print( tup [i] )

Output

The output standard deviation obtained is as follows −

Your tuple is:
‘this’
‘is’
‘a’
‘sample’
‘tuple’

Using for in loop

In this method, we will make use of for-in loop to access each element of the tuple and then print them one by one on the screen.

Example

In this example we will make a for-in loop in a tuple and in each iteration, the ‘i’ will take the element of the tuple and we will print it using the print function.

tup = ( 'this' , 'is' , 'a' , 'sample' , 'tuple' )
print ("your tuple is: ")
for i in tup:
   print( i , sep = " " )

Output

The output standard deviation obtained is as follows −

your tuple is: 
this
is
a
sample
tuple

Using index to print a specific element

In this method we will make use of index to print only one of the element of the tuple at one time.

Example

In this example we will make use of index of the tuple to print the element at that index of the tuple.

tup = ( 'this' , 'is' , 'a' , 'sample' , 'tuple' )
print ("element at the index 0 is: " , tup[0])
print ("element at the index 1 is: " , tup[1])
print ("element at the index 3 is: " , tup[3])

Output

The output standard deviation obtained is as follows −

element at the index 0 is: ‘this’
element at the index 1 is: ‘is’
element at the index 3 is: ‘sample’

Using string formatting

In this method, we will make use of string formatting to print the tuple at once.

Example

In this example, we will make use of string formatting to print the tuple along with a string. String formatting is the process of inserting a custom string or variable in predefined text.

tup = ( 'this' , 'is' , 'a' , 'sample' , 'tuple' )
result = f'your tuple is: {tup}'
print(result)

Output

The output standard deviation obtained is as follows −

Your tuple is: ( ‘this’ , ‘is’ , ‘a’ , ‘sample’ , ‘tuple’ )

Conclusion

In this article, we discussed about tuples how we can create a tuple and what are its properties. We came to see about 5 different methods to print the elements of a tuple. In the first method, we used the print function to print the whole tuple at once. In the second method, we used the indexing method to loop over the length of the tuple and print each elements of the tuple

In the third method, we used a for in loop to iterate over the tuple where the iterator assumes every value of the tuple and prints it one by one. In the fourth method we used an index to print elements of the tuple at a specific index. In the fifth method, we used the string formatting method to print the tuple inside.

Updated on: 17-Feb-2023

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements