How To Print Tuple Elements in Python

Tuple in Python helps you store many elements in a specific variable. It is possible to print tuple elements in Python. In this article, we will go together to learn several ways to do this work.

Ways to print tuple elements in Python

Here is how you print tuple elements in Python.

Print the entire tuple containing all elements 

The print() function helps you print any data type, including a tuple with its elements. 

Here is the syntax:

print([variable])

In which:

[variable] = ([element 1], [element 2, …, [element N]])

You create a [variable] that takes the tuple data type. The [variable] stores many elements of different data types. After that, you need to put your tuple as the parameter of the print() function. The output will be all tuple elements inside a ().

See the code below:

tupItems = ("Learn", "Share", "It")
print(tupItems)

The output will be:

('Learn', 'Share', 'It')

Use for loop to print tuple elements

You want to print all tuple elements on the screen without the bracket (). This work is possible, all thanks to the for loop.

The syntax is like this:

for element in [variable]:
    print(element)

In which:

[variable] = ([element 1], [element 2, …, [element N]])

The for loop will visit each element in the tuple and print that out. The printed element is in string format.

Here is the code sample:

tupItems = ("Learn", "Share", "It")
for element in tupItems:
  print(element)

The output will be:

Learn
Share
It

You can use end=" " as the second parameter of the print() function. This will help you print all tuple elements in a single line.

The code sample is below:

tupItems = ("Learn", "Share", "It")
for element in tupItems:    
  print(element, end=" ")

The output will be:

Learn Share It

Print a single element in the tuple

Tuple in Python has its elements ordered. Therefore, you can access each element in the tuple through its index. This means you can print any element in the tuple as you desire.

The syntax is like this:

print([variable][index])

In which:

[variable] = ([element 1], [element 2, …, [element N]])

The [variable][index] means the element that has the [index] position in the tuple. Please keep in mind that the index in the tuple starts from 0. For example, if you write [variable][2], you refer to the third element in the tuple.

Here is the code sample:

tupItems = ("Learn", "Share", "It")
print(tupItems[1])

As the “Share” has the index 1 in the tuple tupItems, the output will be:

Share

Print tuple elements in a desired range

This method is the advanced version of the previous way. Since tuple elements in Python are ordered in fixed positions, you can not only print a single element but also many elements in the desired range.

The syntax is like this:

print([variable][index 1]:[index 2])

In which:

[variable] = ([element 1], [element 2, …, [element N]])

Please make sure that the [index 2] is bigger than [index 1]. The command above will print a new tuple containing only elements in the range from the [index 1] to the [index 2] of the [variable].

See the code sample below:

tupItems = ("Learn", "Share", "It")
print(tupItems[0:2])

The output will be:

('Learn', 'Share')

Summary

To summarize, we have shared with you 4 ways to print tuple elements in Python. You can print the entire tuple with () or without () (using loop). Also, there are ways for you to print a single element in the tuple, or many elements in the desired range, by accessing the index of the elements.

Leave a Reply

Your email address will not be published. Required fields are marked *