Split a string in equal parts (grouper in Python)

In this tutorial, we will learn how to split a string into equal-sized parts using Python. This technique is often called a "grouper" function and is useful for processing data in chunks.

Problem Overview

Given a string and a desired chunk size, we want to divide the string into equal parts. If the string length is not evenly divisible, we'll pad the last chunk with a fill character.

Example 1

Split 'Tutorialspoint' into chunks of 5 characters ?

string = 'Tutorialspoint'
each_part_length = 5
print(f"Input: '{string}' with chunk size {each_part_length}")
Input: 'Tutorialspoint' with chunk size 5

Expected output: Tutor ialsp ointX

Example 2

Split 'Tutorialspoint' into chunks of 6 characters ?

string = 'Tutorialspoint'
each_part_length = 6
print(f"Input: '{string}' with chunk size {each_part_length}")
Input: 'Tutorialspoint' with chunk size 6

Expected output: Tutori alspoi ntXXXX

Using itertools.zip_longest()

The zip_longest() method from the itertools module is perfect for this task. It takes multiple iterators and groups elements together, padding shorter iterators with a fill value.

How It Works

We create multiple iterators from the same string, then use zip_longest() to group characters. Each iterator advances independently, effectively creating chunks.

import itertools

def split_string_equal_parts(string, chunk_size, fill_char='X'):
    # Create multiple iterators from the same string
    iterators = [iter(string)] * chunk_size
    
    # Group characters using zip_longest
    chunks = itertools.zip_longest(*iterators, fillvalue=fill_char)
    
    # Convert tuples to strings and join with spaces
    result = ' '.join(''.join(chunk) for chunk in chunks)
    return result

# Example 1: chunk size 5
string = 'Tutorialspoint'
result1 = split_string_equal_parts(string, 5)
print(f"Chunk size 5: {result1}")

# Example 2: chunk size 6  
result2 = split_string_equal_parts(string, 6)
print(f"Chunk size 6: {result2}")
Chunk size 5: Tutor ialsp ointX
Chunk size 6: Tutori alspoi ntXXXX

Step-by-Step Breakdown

Let's examine how the algorithm works with a smaller example ?

import itertools

string = 'ABCDEFGH'
chunk_size = 3

# Step 1: Create multiple iterators
iterators = [iter(string)] * chunk_size
print(f"Created {len(iterators)} iterators from '{string}'")

# Step 2: Use zip_longest to group
chunks = list(itertools.zip_longest(*iterators, fillvalue='X'))
print(f"Chunks as tuples: {chunks}")

# Step 3: Convert to strings
result = ' '.join(''.join(chunk) for chunk in chunks)
print(f"Final result: {result}")
Created 3 iterators from 'ABCDEFGH'
Chunks as tuples: [('A', 'B', 'C'), ('D', 'E', 'F'), ('G', 'H', 'X')]
Final result: ABC DEF GHX

Alternative Approaches

Using List Slicing

A simpler approach using string slicing ?

def split_with_slicing(string, chunk_size, fill_char='X'):
    # Pad string to make it divisible by chunk_size
    padding_needed = (chunk_size - len(string) % chunk_size) % chunk_size
    padded_string = string + fill_char * padding_needed
    
    # Create chunks using slicing
    chunks = [padded_string[i:i+chunk_size] for i in range(0, len(padded_string), chunk_size)]
    return ' '.join(chunks)

string = 'Tutorialspoint'
result = split_with_slicing(string, 5)
print(f"Using slicing: {result}")
Using slicing: Tutor ialsp ointX

Comparison

Method Complexity Memory Usage Best For
zip_longest() Medium Iterator-based Large strings, memory efficiency
List slicing Simple Creates string copy Small strings, readability

Conclusion

Use itertools.zip_longest() for memory-efficient string chunking with automatic padding. The slicing approach offers simpler code for smaller strings. Both methods effectively split strings into equal-sized parts.

Updated on: 2026-03-25T09:02:47+05:30

411 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements