Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Reverse Sort a String - Python
Next article icon

Python | Reverse Interval Slicing String

Last Updated : 29 Mar, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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)) 

Output
The 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))  

Output
The 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))  

Output
The 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 

Output
The 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 

Output
The original string is : geeksforgeeks The reverse Interval Slice : segoseg

Time Complexity: O(n)
Auxiliary Space: O(n)


Next Article
Reverse Sort a String - Python
author
manjeet_04
Improve
Article Tags :
  • Python
  • Python Programs
  • Python string-programs
Practice Tags :
  • python

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences