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";
In this article, we will see 3 different methods of “How to Print Tuple Elements in Python”.
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.
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 )
The obtained output standard deviation is as follows -
Your tuple is: ( ‘this’ , ‘is’ , ‘a’ , ‘sample’ , ‘tuple’ )
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.
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] )
The obtained output standard deviation is as follows -
Your tuple is: ‘this’ ‘is’ ‘a’ ‘sample’ ‘tuple’
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.
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 = " " )
The obtained output standard deviation is as follows -
your tuple is: this is a sample tuple
In this method, we will print only one element of the tuple at a time using the index.
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])
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’
In this method, we will use string format to print the tuple immediately.
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)
The obtained output standard deviation is as follows -
Your tuple is: ( ‘this’ , ‘is’ , ‘a’ , ‘sample’ , ‘tuple’ )
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!