Home > Article > Backend Development > Python program to divide a string into N equal length parts
In Python, you can use the slicing method to divide a string into N equal parts. Substrings of equal length can be extracted by specifying the starting and ending indices of the substring. In this article, we will see how to split a string into N equal parts using Python slicing method.
To divide a string into N equal parts, we need to create a function that accepts the original string and the number of parts to be divided into as input, and returns the result as N equal strings. If the string contains some extra characters that cannot be assigned to N equal parts, they are added to the last substring.
The Chinese translation ofIn the example code below, we create a method called divide_string that accepts as input the original string and the number of parts to divide the string into, and returns N equal substring as output. The function divide_string performs the following operations −
Calculate the length of each substring by dividing the length of the original string by the number of parts (N).
Use list comprehensions to divide the string into N parts. We start at index 0 and move by steps part_length(string length/N) until we reach the end of the string.
If there are any extra remaining characters that were not added to the substring, we add them to the last substring part.
Return 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)
['abc', 'def', 'ghi']The Chinese translation of
In the example below, the length of the string is 26, and it needs to be divided into 6 equal parts. Therefore, the length of each substring is 4. But after splitting the string into 6 parts, 2 characters of the string are redundant and 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)
['Welc', 'ome ', 'to t', 'utor', 'ials', ' point']
In this article, we learned how to use Python’s slicing function to divide a string into N equal parts. The length of each substring is calculated by dividing the length of the string by N. If there are any remaining characters after the string is split, they will be added to the last substring. This is an efficient way to split a string into N equal parts.
The above is the detailed content of Python program to divide a string into N equal length parts. For more information, please follow other related articles on the PHP Chinese website!