Python Program to split string into k sized overlapping strings
Last Updated :
22 Apr, 2023
Given a string, the task is to write a Python program to extract overlapping consecutive string slices from the original string according to size K.
Example:
Input : test_str = 'Geeksforgeeks', K = 4
Output : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
Explanation : Consecutive overlapping 4 sized strings are output.
Input : test_str = 'Geeksforgeeks', K = 6
Output : ['Geeksf', 'eeksfo', 'eksfor', 'ksforg', 'sforge', 'forgee', 'orgeek', 'rgeeks']
Explanation : Consecutive overlapping 6 sized strings are output.
Method 1: Using islice() + generator function + join()
In this, windows of size K are extracted using the islice(), and results are yielded in an intermediate way using yield. The final results are joined using join().
Python3
# Python3 code to demonstrate working of
# Overlapping consecutive K splits
# Using islice() + generator function + join()
from itertools import islice
# generator function
def over_slice(test_str, K):
itr = iter(test_str)
res = tuple(islice(itr, K))
if len(res) == K:
yield res
for ele in itr:
res = res[1:] + (ele,)
yield res
# initializing string
test_str = 'Geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = 4
# calling generator function
res = ["".join(ele) for ele in over_slice(test_str, K)]
# printing result
print("Overlapping windows : " + str(res))
Output:
The original string is : Geeksforgeeks
Overlapping windows : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
Method 2: Using list comprehension + slicing
In this example, intermediate slices are performed using a slicing operation in a more pythonic way. Each window is extracted using slice notation.
Python3
# Python3 code to demonstrate working of
# Overlapping consecutive K splits
# Using list comprehension + slicing
# initializing string
test_str = 'Geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = 4
# extracting window using slicing
res = [test_str[idx:idx + K] for idx in range(len(test_str) - K + 1)]
# printing result
print("Overlapping windows : " + str(res))
Output:
The original string is : Geeksforgeeks
Overlapping windows : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
The Time and Space Complexity of all the methods is :
Time Complexity: O(n)
Space Complexity: O(n)
Method 3: Using recursion :
Python3
# Python3 code to demonstrate working of
# Overlapping consecutive K splits
# Using recursion
# initializing string
test_str = 'Geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = 4
# recursive function
def over_slice(test_str, K, res = []):
# if length of string is less
if (len(test_str) < K):
return
# append the string
res.append(test_str[:K])
# call recursively
over_slice(test_str[1:], K, res)
# return result
return res
# calling recursive function
res = over_slice(test_str, K)
# printing result
print("Overlapping windows : " + str(res))
#This code is contributed by Edula Vinay Kumar Reddy
OutputThe original string is : Geeksforgeeks
Overlapping windows : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method 4 : using the windowed() function from the more_itertools module.
This function takes an iterable and a window size, and returns an iterator over overlapping windows of the specified size.
The steps for this approach are:
Import the windowed() function from the more_itertools module.
Initialize a list containing the string to be split.
Use the windowed() function to get an iterator over overlapping windows of size K.
Use a list comprehension to join each window of characters into a string.
Return the list of overlapping windows.
Python3
# Python3 code to demonstrate working of
# Overlapping consecutive K splits
# Using windowed() function from more_itertools module
from more_itertools import windowed
# initializing string
test_str = 'Geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing K
K = 4
# converting string to list of characters
str_list = list(test_str)
# using windowed() function to get overlapping windows
windows = windowed(str_list, K, step=1)
# using list comprehension to join windows into strings
res = ["".join(w) for w in windows]
# printing result
print("Overlapping windows : " + str(res))
OUTPUT :
The original string is : Geeksforgeeks
Overlapping windows : ['Geek', 'eeks', 'eksf', 'ksfo', 'sfor', 'forg', 'orge', 'rgee', 'geek', 'eeks']
The time complexity of this approach is O(N), since the windowed() function is a generator and only generates the windows as they are needed, without precomputing them all at once.
The auxiliary space complexity is O(NK), since we need to store all the overlapping windows in a list.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read