Python Program to Split a String by Custom Lengths

In this article, we will learn how to split a string by custom lengths in Python. This technique is useful when you need to break a string into segments of specific sizes, such as parsing fixed-width data formats or creating chunks for processing.

Methods Used

The following are the various methods to accomplish this task ?

  • Using for loop and slicing

  • Using List Comprehension with iter() and next() functions

Problem Overview

Let's understand the problem with an example. Assume we have an input string and custom lengths list. We will split the input string according to the specified lengths ?

Input

inputString = 'hitutorialspoint'
customLengths = [4, 1, 6, 5]
print(f"String: {inputString}")
print(f"Lengths: {customLengths}")
String: hitutorialspoint
Lengths: [4, 1, 6, 5]

Expected Output

['hitu', 't', 'orials', 'point']

The string is split based on the given custom lengths ?

  • 4 characters ? 'hitu'

  • 1 character ? 't'

  • 6 characters ? 'orials'

  • 5 characters ? 'point'

Method 1: Using For Loop and Slicing

This method uses string slicing with a loop to extract segments of the specified lengths ?

# input string
inputString = 'hitutorialspoint'
print("Input string:", inputString)

# input custom lengths list
customLengths = [4, 1, 6, 5]

# empty list for storing results
outputList = []

# initializing start index as 0
startIndex = 0

# traversing through each element of the custom lengths list
for length in customLengths:
    # appending the custom length string sliced from the starting index
    outputList.append(inputString[startIndex: startIndex + length])
    
    # increment the start index value with the custom length element
    startIndex += length

# printing the resultant output list
print("Resultant list after splitting by custom lengths:")
print(outputList)
Input string: hitutorialspoint
Resultant list after splitting by custom lengths:
['hitu', 't', 'orials', 'point']

Method 2: Using List Comprehension with iter() and next()

This method uses an iterator to traverse the string and extract characters using the next() function ?

Key Functions

  • iter() ? Creates an iterator object from the string

  • next() ? Returns the next item from the iterator

  • join() ? Joins characters into a string

# input string
inputString = 'hitutorialspoint'
print("Input string:", inputString)

# input custom lengths list
customLengths = [4, 1, 6, 5]

# getting the string as an iterator object
stringIterator = iter(inputString)

# using list comprehension with next() and join()
outputList = ["".join(next(stringIterator) for i in range(length)) for length in customLengths]

# printing the resultant output list
print("Resultant list after splitting by custom lengths:")
print(outputList)
Input string: hitutorialspoint
Resultant list after splitting by custom lengths:
['hitu', 't', 'orials', 'point']

Comparison

Method Readability Performance Best For
For Loop + Slicing High Good Beginners, clear logic
List Comprehension + Iterator Medium Better Compact code, advanced users

Conclusion

Both methods effectively split strings by custom lengths. Use the for loop approach for clarity and the iterator method for more compact, Pythonic code. The iterator approach is memory-efficient for large strings as it processes characters one at a time.

Updated on: 2026-03-27T00:03:41+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements