Python – Divide String into Equal K chunks
Last Updated :
06 Apr, 2023
Given a String perform division into K equal chunks.
Input : test_str = ‘geeksforgeek’, K = 4
Output : [‘gee’, ‘ksf’, ‘org’, ‘eek’]
Explanation : 12/4 = 3, length of each string extracted.
Input : test_str = ‘geeksforgeek’, K = 1
Output : [‘geeksforgeek’]
Explanation : 12/1 = 12, whole string is single chunk.
Method #1: Using len() + loop
In this, we first perform task of computation of length of each chunk required from K and string length, post that, string is splitted on desired indices to extract chunks using slicing.
Python3
test_str = 'geeksforgeeks 1'
print ( "The original string is : " + str (test_str))
K = 5
chnk_len = len (test_str) / / K
res = []
for idx in range ( 0 , len (test_str), chnk_len):
res.append(test_str[idx : idx + chnk_len])
print ( "The K chunked list : " + str (res))
|
Output
The original string is : geeksforgeeks 1
The K chunked list : ['gee', 'ksf', 'org', 'eek', 's 1']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using list comprehension
The method similar to above, difference being that last process is encapsulated to one-liner list comprehension.
Python3
test_str = 'geeksforgeeks 1'
print ( "The original string is : " + str (test_str))
K = 5
chnk_len = len (test_str) / / K
res = [test_str[idx : idx + chnk_len] for idx in range ( 0 , len (test_str), chnk_len)]
print ( "The K len chunked list : " + str (res))
|
Output
The original string is : geeksforgeeks 1
The K len chunked list : ['gee', 'ksf', 'org', 'eek', 's 1']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using the string slicing technique in a loop
This solution uses a loop and string slicing to divide the string into equal chunks of length chnk_len. The for loop iterates through the string with a step size of chnk_len, and slices the string into chunks of length chnk_len. The chunks are then appended to an empty list res. Finally, the list res is printed to display the chunks.
Python3
test_str = 'geeksforgeeks 1'
print ( "The original string is : " + str (test_str))
K = 5
chnk_len = len (test_str) / / K
res = []
for i in range ( 0 , len (test_str), chnk_len):
res.append(test_str[i:i + chnk_len])
print ( "The K len chunked list : " + str (res))
|
Output
The original string is : geeksforgeeks 1
The K len chunked list : ['gee', 'ksf', 'org', 'eek', 's 1']
Time complexity: O(n), where n is the length of the input string test_str.
Auxiliary space: O(k), where k is the number of chunks.
Method 4: Using itertools.islice()
- Import the itertools module.
- Initialize an empty list to store the chunks.
- Compute the chunk length chnk_len by dividing the length of the input string by K.
- Use the itertools.islice() method to iterate through the input string in chunks of length chnk_len.
- Append each chunk to the list of chunks.
- If the length of the input string is not evenly divisible by K, there will be one final chunk of smaller length.
- Append this final chunk to the list of chunks.
- Return the list of chunks.
Python3
test_str = 'geeksforgeeks 1'
print ( "The original string is : " + str (test_str))
K = 5
chnk_len = len (test_str) / / K
res = []
for i in range ( 0 , len (test_str), chnk_len):
res.append(test_str[i:i + chnk_len])
if len (test_str) % K ! = 0 :
res.append(test_str[ - ( len (test_str) % chnk_len):])
print ( "The K len chunked list : " + str (res))
|
Output
The original string is : geeksforgeeks 1
The K len chunked list : ['gee', 'ksf', 'org', 'eek', 's 1']
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(k), where k is the number of chunks.
Method #5: Using regular expression
Steps:
- Import the re module to use the regular expression.
- Initialize the string and the value of K.
- Compute the length of each chunk.
- Use re.findall() to split the string into equal chunks of length chnk_len.
- Print the resulting list of chunks.
Python3
import re
test_str = 'geeksforgeeks 1'
print ( "The original string is : " + str (test_str))
K = 5
chnk_len = len (test_str) / / K
res = re.findall( '.' * chnk_len, test_str)
print ( "The K len chunked list : " + str (res))
|
Output
The original string is : geeksforgeeks 1
The K len chunked list : ['gee', 'ksf', 'org', 'eek', 's 1']
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n/K), where K is the number of equal chunks.
Share your thoughts in the comments
Please Login to comment...