Skip to content
Related Articles

Related Articles

Python | Reverse Incremental String Slicing

View Discussion
Improve Article
Save Article
  • Last Updated : 23 Jun, 2022

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 application in day-day programming. Let us discuss certain ways in which this task can be performed.

Method #1: Using loops: 

This is the brute force way in which this task can be performed. In this, we iterate the list in reverse order and store the incremental strings in the list.

Python3




# Python3 code to demonstrate working of
# Reverse Incremental String Slicing
# Using loop
 
# initializing string
test_str = "
geeks & quot
 
# printing original string
print(& quot
       The original string is : & quot
       + test_str)
 
# Reverse Incremental String Slicing
# Using loop
res = []
sub = ''
for chr in reversed(test_str):
    sub += chr
    res.append(sub)
 
# printing result
print(& quot
       The incremental reverse strings : & quot
       + str(res))

Output : 

The original string is : geeks
The incremental reverse strings : ['s', 'sk', 'ske', 'skee', 'skeeg']

 

 
Method #2: Using list slicing + list comprehension 

This is yet another way in which this task can be performed. In this, we iterate the string list using list comprehension and slicing is used to perform incremental slicing.

Python3




# Python3 code to demonstrate working of
# Reverse Incremental String Slicing
# Using list comprehension + list slicing
 
# initializing string
test_str = "
geeks & quot
 
# printing original string
print(& quot
       The original string is : & quot
       + test_str)
 
# Reverse Incremental String Slicing
# Using list comprehension + list slicing
res = [test_str[-1: idx: -1] for idx in range(-2, -2 - len(test_str), -1)]
 
# printing result
print(& quot
       The incremental reverse strings : & quot
       + str(res))

Output : 

The original string is : geeks
The incremental reverse strings : ['s', 'sk', 'ske', 'skee', 'skeeg']

 

Method#3: Using accumulate + string slicing 

This is another way to perform the task. We can use string slicing to reverse the string and accumulate is used to iterate over the string and slice the string in sub-strings.

Python3




# Python3 code to demonstrate working of
# Reverse Incremental String Slicing
# Using accumulate + string slicing
 
from itertools import accumulate
import operator
 
# initializing string
test_str = "geeks"
 
# printing original string
print("The original string is : " + test_str)
 
 
# Reverse Incremental String Slicing
# Using accumulate + string slicing
ans = list(accumulate(test_str[::-1], operator.add))
 
# printing result
print("The incremental reverse strings : " + str(ans))

Output

The original string is : geeks
The incremental reverse strings : ['s', 'sk', 'ske', 'skee', 'skeeg']

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!