Python Program to divide a string in \'N\' equal parts

In Python, a string can be divided into N equal parts using the slicing method. Substrings of equal length can be extracted using the Python slicing method by specifying the starting and ending index of the substring. In this article, we will see how we can divide a string into N equal parts using the Python slicing method.

To divide a string into N equal parts we need to create a function that takes the original string and the number of parts in which the string is to be divided as input and returns the resultant N equal strings. If the string contains a few extra characters which cannot be distributed in the N equal parts, we add them to the last substring.

Approach

The algorithm follows these steps ?

  • Calculate the length of each substring by dividing the length of the original string by the number of parts (N)

  • Use list comprehension to divide the string into parts, starting with index 0 and moving with the step of part_length

  • If there are any extra leftover characters, add them to the last substring

  • Return the N substrings

Example 1: Perfect Division

When the string length is perfectly divisible by N ?

def divide_string(string, parts):
    # Determine the length of each substring
    part_length = len(string) // parts
    
    # Divide the string into 'parts' number of substrings
    substrings = [string[i:i + part_length] for i in range(0, len(string), part_length)]
    
    # If there are any leftover characters, add them to the last substring
    if len(substrings) > parts:
        substrings[-2] += substrings[-1]
        substrings.pop()
    
    return substrings

string = "abcdefghi"
parts = 3

result = divide_string(string, parts)
print(f"Original string: {string}")
print(f"Divided into {parts} parts: {result}")
Original string: abcdefghi
Divided into 3 parts: ['abc', 'def', 'ghi']

Example 2: With Leftover Characters

When the string length is not perfectly divisible, extra characters are added to the last part ?

def divide_string(string, parts):
    # Determine the length of each substring
    part_length = len(string) // parts
    
    # Divide the string into 'parts' number of substrings
    substrings = [string[i:i + part_length] for i in range(0, len(string), part_length)]
    
    # If there are any leftover characters, add them to the last substring
    if len(substrings) > parts:
        substrings[-2] += substrings[-1]
        substrings.pop()
    
    return substrings

string = "Welcome to tutorials point"
parts = 6

result = divide_string(string, parts)
print(f"Original string: '{string}' (length: {len(string)})")
print(f"Divided into {parts} parts: {result}")
print(f"Part lengths: {[len(part) for part in result]}")
Original string: 'Welcome to tutorials point' (length: 26)
Divided into 6 parts: ['Welc', 'ome ', 'to t', 'utor', 'ials', ' point']
Part lengths: [4, 4, 4, 4, 4, 6]

Alternative Approach: Using Math

A more explicit approach using mathematical calculation ?

def divide_string_v2(string, parts):
    length = len(string)
    part_size = length // parts
    remainder = length % parts
    
    result = []
    start = 0
    
    for i in range(parts):
        # Add one extra character to the last part if there's a remainder
        end = start + part_size + (1 if i == parts - 1 and remainder > 0 else 0)
        result.append(string[start:end])
        start = end - (remainder if i == parts - 1 and remainder > 0 else 0)
    
    # Handle any remaining characters by adding to last part
    if start < length:
        result[-1] += string[start:]
    
    return result

string = "PythonProgramming"
parts = 5

result = divide_string_v2(string, parts)
print(f"String: {string}")
print(f"Divided: {result}")
String: PythonProgramming
Divided: ['Pyt', 'hon', 'Pro', 'gra', 'mming']

Conclusion

String division into N equal parts is efficiently achieved using Python slicing. The basic approach calculates part length using integer division and handles leftover characters by appending them to the last substring, ensuring no data is lost in the process.

Updated on: 2026-03-27T01:12:43+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements