Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Reverse Slicing of given string

Python | Reverse Slicing of given string

Sometimes, while working with strings we might have a problem in which we need to perform the reverse slicing of string, i.e slicing the string for certain characters from the rear end. Let’s discuss certain ways in which this can be done.

Method #1 : Using join() + reversed() The combination of above function can be used to perform this particular task. In this, we reverse the string in memory and join the sliced no. of characters so as to return the string sliced from rear end. 

Python3




# Python3 code to demonstrate working of
# Reverse Slicing string
# Using join() + reversed()
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 7
 
# Using join() + reversed()
# Reverse Slicing string
res = ''.join(reversed(test_str[0:K]))
 
# printing result
print("The reversed sliced string is : " + res)


Output : 

The original string is : Lazyroar
The reversed sliced string is : ofskeeG

Method #2 : Using string slicing The string slicing can be used to perform this particular task, by using “-1” as the third argument in slicing we can make function perform the slicing from rear end hence proving to be a simple solution. 

Python3




# Python3 code to demonstrate working of
# Reverse Slicing string
# Using string slicing
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 7
 
# Using string slicing
# Reverse Slicing string
res = test_str[(K-1)::-1]
 
# printing result
print("The reversed sliced string is : " + res)


Output : 

The original string is : Lazyroar
The reversed sliced string is : ofskeeG

Method#3: Using another way of slicing In this way of slicing we first reverse the string and then we split the string with the help of string slicing and we can first slicing the string then reverse the string.

Python3




# Python3 code to demonstrate working of
# Reverse Slicing string
# Using string slicing
 
# initializing string
test_str = "Lazyroar"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = 7
 
# Using string slicing
# Reverse Slicing string
ans = test_str[::-1][K-1:]
ans2 = test_str[:K][::-1]
 
# printing result
print("The sliced reversed string is : " + ans)
print("Reversed sliced the string is : " + ans2)


Output:

The original string is : Lazyroar
The sliced reversed string is : ofskeeG
Reversed sliced the string is : ofskeeG 

Method#4: using a list comprehension and join()

step-by-step algorithm for the approach:

  1. Initialize a string test_str.
  2. Specify an integer k indicating the starting index for reversing.
  3. Use list comprehension to create a list of characters in reverse order, starting from index k-1 down to 0. The list is assigned to rev_chars.
  4. Join the list of characters into a single string, and assign it to rev_str.
  5. Print the reversed sliced string.

Python3




# initializing string
test_str = "Lazyroar"
 
# specify the index where to start reversing
k = 7
 
# using list comprehension to create a list of characters in reverse order
# starting from index k-1 down to 0
rev_chars = [test_str[i] for i in range(k-1, -1, -1)]
 
# join the list of characters into a single string
rev_str = ''.join(rev_chars)
 
# print the reversed sliced string
print(rev_str)


Output

ofskeeG

Time complexity: The time complexity of the algorithm is O(k), where k is the index where the reversal starts. In the worst case, where k is at the beginning of the string, the algorithm will take linear time to complete.

Auxiliary space: The space complexity of the algorithm is O(k), where k is the index where the reversal starts. This is because the algorithm creates a new list of characters in reverse order, which takes up memory proportional to the length of the slice being reversed. However, since rev_chars and rev_str are the only variables that grow with the input size, the space complexity can also be described as O(n), where n is the length of the input string.

Method #5: Using a for loop and concatenation

 step-by-step approach 

  1. Initialize a string variable test_str with the value “Lazyroar”.
  2. Initialize an integer variable k with the value 7.
  3. Initialize an empty string variable rev_str.
  4. Start a for loop to iterate over a range of numbers. The range function is used to generate a sequence of numbers starting from k-1 down to 0, in reverse order. The sequence of numbers is generated using the parameters (k-1, -1, -1) passed to the range function.
  5. For each number i in the sequence generated by the range function, the corresponding character of test_str at index i is appended to the rev_str variable using the += operator.
  6. After the loop completes, the reversed sliced string is stored in the rev_str variable.
  7. The reversed sliced string is printed using the print function.
  8. The program reverses a slice of the original string starting from index k-1 and going backwards

Python3




# initializing string
test_str = "Lazyroar"
 
# specify the index where to start reversing
k = 7
 
# initialize an empty string to store the reversed characters
rev_str = ''
 
# iterate over the characters of the string in reverse order
# starting from index k-1 down to 0 using a for loop
for i in range(k-1, -1, -1):
    rev_str += test_str[i]
 
# print the reversed sliced string
print(rev_str)


Output

ofskeeG

Time complexity: O(k), where k is the index where to start reversing.

Auxiliary space: O(k), where k is the index where to start reversing (for storing the reversed characters).

RELATED ARTICLES

Most Popular

Recent Comments