Skip to content
Related Articles

Related Articles

Python Program to split string into k sized overlapping strings

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 07 Aug, 2022
Improve Article
Save Article

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)


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!