Python - Split a String by Custom Lengths
Last Updated :
17 Apr, 2023
Given a String, perform split of strings on the basis of custom lengths.
Input : test_str = 'geeksforgeeks', cus_lens = [4, 3, 2, 3, 1]
Output : ['geek', 'sfo', 'rg', 'eek', 's']
Explanation : Strings separated by custom lengths.
Input : test_str = 'geeksforgeeks', cus_lens = [10, 3]
Output : ['geeksforge', 'eks']
Explanation : Strings separated by custom lengths.
Method #1 : Using slicing + loop
In this, we perform task of slicing to cater custom lengths and loop is used to iterate for all the lengths.
Python3
# Python3 code to demonstrate working of
# Multilength String Split
# Using loop + slicing
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing length list
cus_lens = [5, 3, 2, 3]
res = []
strt = 0
for size in cus_lens:
# slicing for particular length
res.append(test_str[strt : strt + size])
strt += size
# printing result
print("Strings after splitting : " + str(res))
OutputThe original string is : geeksforgeeks
Strings after splitting : ['geeks', 'for', 'ge', 'eks']
Time Complexity: O(n), where n is the length of the string
Auxiliary Space: O(n), length of the res string
Method #2 : Using join() + list comprehension + next()
This is yet another way in which this task can be performed. In this, we perform task of getting character till length using next(), iterator method, provides more efficient solution. Lastly, join() is used to convert each character list to string.
Python3
# Python3 code to demonstrate working of
# Multilength String Split
# Using join() + list comprehension + next()
# initializing string
test_str = 'geeksforgeeks'
# printing original string
print("The original string is : " + str(test_str))
# initializing length list
cus_lens = [5, 3, 2, 3]
# join() performs characters to string conversion
# list comprehension provides shorthand to solve problem
stritr = iter(test_str)
res = ["".join(next(stritr) for idx in range(size)) for size in cus_lens]
# printing result
print("Strings after splitting : " + str(res))
OutputThe original string is : geeksforgeeks
Strings after splitting : ['geeks', 'for', 'ge', 'eks']
The Time and Space Complexity for all the methods are the same:
Time Complexity: O(n)
Space Complexity: O(n)
Similar Reads
Split a String by a Delimiter in Python In Python Programming, the split() method is used to split a string into a list of substrings which is based on the specified delimiter. This method takes the delimiter as an argument and returns the list of substrings. Along with this method, we can use various approaches wot split a string by the
2 min read
Python | Custom list split Development and sometimes machine learning applications require splitting lists into smaller list in a custom way, i.e on certain values on which split has to be performed. This is quite a useful utility to have knowledge about. Let's discuss certain ways in which this task can be performed. Method
8 min read
Python | Split flatten String List Sometimes, while working with Python Strings, we can have problem in which we need to perform the split of strings on a particular deliminator. In this, we might need to flatten this to a single String List. Let's discuss certain ways in which this task can be performed. Method #1 : Using list compr
7 min read
Split and join a string in Python The goal here is to split a string into smaller parts based on a delimiter and then join those parts back together with a different delimiter. For example, given the string "Hello, how are you?", you might want to split it by spaces to get a list of individual words and then join them back together
3 min read
Python - Avoid Spaces in string length When working with strings in Python, we may sometimes need to calculate the length of a string excluding spaces. The presence of spaces can skew the length when we're only interested in the number of non-space characters. Let's explore different methods to find the length of a string while ignoring
3 min read
Python - Custom length tuples from String Given a String, extract tuple list, with each tuple being of custom length, delimited using comma. Input : test_str = "6 6 7, 3 4, 2" Output : [(6, 6, 7), (3, 4), (2, )] Explanation : The customs lengths being 3, 2, 1 have been converted to tuple list. Input : test_str = "7, 7, 4" Output : [(7, ), (
4 min read