Example:
tuple = ("python", "includehelp", 43, 54.23)
Extracting rear elements from tuple String
In this program, we have a tuple with string elements. And we need to create a python program to extract all elements from the tuple of string elements.
Input:
("python", "learn", "includehelp")
Output:
["n", "n", "p"]
Method 1:
One simple way to solve the problem is by traversing the tuple and then for each string element extract the (n-1)th index value. This is done in a string line of code using list comprehension.
Output:
The elements of the tuple string are : ('python', 'learn', 'includehelp') The rear elements of each string from the tuple : ['n', 'n', 'p']Method 2:
Another method to perform the task is by directly using a loop to iterate over each element of the tuple. For each string of the tuple, extract the rear element i.e. element at (n-1)th index.
Output:
The elements of the tuple string are : ('python', 'learn', 'includehelp') The rear elements of each string from the tuple : ['n', 'n', 'p']need an explanation for this answer? contact us directly to get an explanation for this answer