Python | Reverse Interval Slicing String
Last Updated : 29 Mar, 2023
Sometimes, while working with strings, we can have a problem in which we need to perform string slicing. In this, we can have a variant in which we need to perform reverse slicing that too interval. This kind of application can come in day-day programming. Let us discuss certain ways in which this task can be performed.
Method #1: Using String Slicing (1)
This task can be performed using string slicing, that too nested one. In this, the first slice performs the Interval, and the second slice performs reverse.
Example
Python3 # Python3 code to demonstrate working of # Reverse Interval Slicing String # Using String Slicing 1 # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # initializing Interval K = 2 # Reverse Interval Slicing String # Using String Slicing 1 res = test_str[::K][::-1] # printing result print("The reverse Interval Slice : " + str(res))
OutputThe original string is : geeksforgeeks The reverse Interval Slice : segoseg
Method #2: Using String Slicing (2)
It is another way in which this task can be performed. In this, we employ similar way as above, but a different kind of slicing.
Python3 # Python3 code to demonstrate working of # Reverse Interval Slicing String # Using String Slicing 2 # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # initializing Interval K = 2 # Reverse Interval Slicing String # Using String Slicing 1 res = test_str[::-1][::K] # printing result print("The reverse Interval Slice : " + str(res))
OutputThe original string is : geeksforgeeks The reverse Interval Slice : segoseg
Method#3: Using join + list comprehension
With a specified method, we can perform this task. List comprehension is used to iterate over the string and join method is used to join the list which is formed as a product of list comprehension.
Python3 # Python3 code to demonstrate working of # Reverse Interval Slicing String # Using join + list comprehension from functools import reduce # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # initializing Interval K = 2 # Reverse Interval Slicing String # Using join and list comprehension k = "".join( test_str[i] for i in range(len(test_str)-1, -1, -K)) # printing result print("The reverse Interval Slice : " + str(k))
OutputThe original string is : geeksforgeeks The reverse Interval Slice : segoseg
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using Recursion
Here is a recursive approach to perform the reverse interval slicing of a string.
Python3 def reverse_interval_slice(string, k, i, result): if i < len(string): result = string[i] + result return reverse_interval_slice(string, k, i+k, result) return result # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # initializing Interval K = 2 # Reverse Interval Slicing String # Using Recursion result = reverse_interval_slice(test_str, K, 0, "") # printing result print("The reverse Interval Slice : " + result) #This code is contributed by Edula Vinay Kumar Reddy
OutputThe original string is : geeksforgeeks The reverse Interval Slice : segoseg
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #5: Using lambda function
In this method, the lambda function takes two arguments test_str and K, which are the string and the interval size, respectively.
The lambda function uses string slicing to reverse the string in intervals of size K. The test_str[::K] expression gets every Kth element from the string starting from the beginning, and the [::-1] expression reverses the resulting string.
Below is the code for the above method:
Python3 reverse_interval_slicing = lambda test_str, K: test_str[::K][::-1] # initializing string test_str = "geeksforgeeks" # printing original string print("The original string is : " + test_str) # initializing Interval K = 2 # Reverse Interval Slicing String res = reverse_interval_slicing(test_str, K) # printing result print("The reverse Interval Slice : " + str(res)) # Contributed by rishabmalhdijo
OutputThe original string is : geeksforgeeks The reverse Interval Slice : segoseg
Time Complexity: O(n)
Auxiliary Space: O(n)
Similar Reads
Python | Reverse Incremental String Slicing
Sometimes, while working with Python strings, we can have a problem in which we need to perform the slice and print of strings in reverse order. This can have applications in day-day programming. Let us discuss certain ways in which this task can be performed. Method #1: Using loops This is the brut
4 min read
Python - Reverse Slicing of given string
Reverse slicing in Python is a way to access string elements in reverse order using negative steps. Using Slicing ([::-1])Using slicing with [::-1] in Python, we can reverse a string or list. This technique works by specifying a step of -1, which starts at the end of the sequence and moves backward,
1 min read
Reverse Sort a String - Python
The goal is to take a given string and arrange its characters in descending order based on their Unicode values. For example, in the string "geeksforgeeks", the characters will be sorted from highest to lowest, resulting in a new string like "ssrokkggfeeeee". Let's understand different methods to pe
2 min read
How To Reverse A List In Python Using Slicing
In Python, list slicing is a common practice and it is the most used technique for programmers to solve efficient problems. In this article, we will see how we can reverse a list using slicing in Python. Reverse a List Using Slicing in PythonBelow are some of the examples by which we can perform rev
3 min read
Python - Reversed Split Strings
In Python, there are times where we need to split a given string into individual words and reverse the order of these words while preserving the order of characters within each word. For example, given the input string "learn python with gfg", the desired output would be "gfg with python learn". Let
3 min read
Reverse All Strings in String List in Python
We are given a list of strings and our task is to reverse each string in the list while keeping the order of the list itself unchanged. For example, if we have a list like this: ['gfg', 'is', 'best'] then the output will be ['gfg', 'si', 'tseb']. Using For LoopWe can use a for loop to iterate over t
2 min read
Python - Remove after substring in String
Removing everything after a specific substring in a string involves locating the substring and then extracting only the part of the string that precedes it. For example we are given a string s="Hello, this is a sample string" we need to remove the part of string after a particular substring includin
3 min read
Python | Splitting operators in String
Sometimes we have a source string to have certain mathematical statement for computations and we need to split both the numbers and operators as a list of individual elements. Let's discuss certain ways in which this problem can be performed. Method #1 : Using re.split() This task can be solved usin
7 min read
Python - Reverse Range in String List
Given a string list, reverse each element of string list from ith to jth index. Input : test_list = ["Geeksforgeeks", "Best", "Geeks"], i, j = 1, 2 Output : ['ee', 'es', 'ee'] Explanation : Range of strings are extracted. Input : test_list = ["Geeksforgeeks"], i, j = 1, 7 Output : ['eeksfor'] Explan
3 min read
Python - Phrase removal in String
Sometimes, while working with Python strings, we can have a problem in which we need to extract certain words in a string excluding the initial and rear K words. This can have application in many domains including all those include data. Lets discuss certain ways in which this task can be performed.
2 min read