Python program to print elements in a tuple

PHPz
Release: 2023-09-11 08:21:09
forward
1646 people have browsed it

Python program to print elements in a tuple

In Python, Tuple is an important data type used to store collections of items. Sometimes, you may need to print the elements of a tuple to understand or debug your code. In this article, we will discuss how to print elements of a tuple in Python.

We will review the syntax for accessing and printing tuple elements and provide examples of how to do this. 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";
Copy after login

In this article, we will see 3 different methods of “How to Print Tuple Elements in Python”.

Use print() function

In this method, we will use python’s print function to print the entire tuple. The print function accepts one argument and prints it to sys.stdout.

Example

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

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

Output

The obtained output standard deviation is as follows -

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

Use index

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

Example

In this example, we will loop over the length of the given tuple and then print the elements 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] )
Copy after login

Output

The obtained output standard deviation is as follows -

Your tuple is:
‘this’
‘is’
‘a’
‘sample’
‘tuple’
Copy after login

Use for in loop

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

Example

In this example, we will create a for-in loop in the tuple and on each iteration, "i" will get the element of the tuple and print it using the print function.

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

Output

The obtained output standard deviation is as follows -

your tuple is: 
this
is
a
sample
tuple
Copy after login

Print specific elements using index

In this method, we will print only one element of the tuple at a time using the index.

Example

In this example, we will use the index of the tuple to print the element at the 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])
Copy after login

Output

The obtained output standard deviation is as follows -

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

Use string format

In this method, we will use string format to print the tuple immediately.

Example

In this example, we will use the string format to print tuples as well as strings. String formatting is the process of inserting custom strings or variables into predefined text.

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

Output

The obtained output standard deviation is as follows -

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

in conclusion

In this article, we discussed Tuples How to create Tuples and their properties. We found about 5 different ways to print the elements of a tuple. In the first method, we use the print function to print the entire tuple at once. In the second method, we use the index method to loop through the length of tuple and print each element of tuple

In the third method, we use a for in loop to iterate the tuple, where the iterator assumes each value of the tuple and prints them one by one. In the fourth method, we use the index to print the element of the tuple at a specific index. In the fifth method, we use the string formatting method to print the tuple inside.

The above is the detailed content of Python program to print elements in a tuple. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!