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 his 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.

Example 1

In the below example code, we have created a method named divide_string which takes the original string and the number of parts the string is divided into as input and returns the N equal substrings as output. The function divide_string performs the following actions −

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

  • Using list comprehension divide the strings into N parts. We start with index 0 and move with the step of part_length (length_of_string/N) until we reach the end of the string.

  • If there are any extra leftover characters that are not added to the substring we add them to the last substring part.

  • Return the N substrings of equal length.

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(result)

Output

['abc', 'def', 'ghi']

Example 2

In the below example, the string is of length 26 and it needs to be divided into 6 equal parts. So each substring will be of length 4. But after the division of the string in 6 parts 2 characters of the string is extra they are added to the last substring as shown in the output.

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(result)

Output

['Welc', 'ome ', 'to t', 'utor', 'ials', ' point']

Conclusion

In this article, we understood how the string can be divided into N equal parts using Python slicing functionality. The length of each substring is calculated by dividing the length of the string by N and if any leftover characters are left after the string division they are added to the last substring. This is an effective way to divide the string into N equal parts.

Updated on: 17-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements