In the previous article, we have discussed Python Program to Check Evil Number or Not

Given a string and the task is to divide the given string into “N” equal parts.

**Examples:**

**Example1:**

**Input:**

Given string = "aaaabbbbccccddddeeee"

**Output:**

The given string after dividing into 5 equal halves: aaaa bbbb cccc dddd eeee

**Example2:**

**Input:**

Given string = "hellobtechgeeks"

**Output:**

The given string cannot be divided into 4 equal halves

## Program to Divide a String in ‘N’ Equal Parts

Below are the ways to divide the given string into “N” equal parts.

### Method #1: Using For Loop (Static Input)

**Approach:**

- Give the string as static input and store it in a variable.
- Give the number say ‘n’ as static input and store it in another variable.
- Calculate the len of the given string using the len() function and store it in another variable.
- Check if the length of the string modulus given number is not equal to ‘0’ or not using the if conditional statement.
- If the statement is true, print “The given string cannot be divided into n equal halves”.
- Else loop from 0 to length of the string with the step size of given number ‘n’ using the for loop.
- Slice from the iterator value to the iterator +n value using slicing and print them.
- The Exit of the Program.

**Below is the implementation:**

# Give the string as static input and store it in a variable. gvn_str = "aaaabbbbccccddddeeee" # Give the number say 'n' as static input and store it in another variable. num = 5 # Calculate the len of the given string using the len() function and store it # in another variable. len_str = len(gvn_str) # Divide the length of the string by a given number and store it in another variable say 'k'. k = len_str//num # Check if the length of the string modulus given number is not equal to '0' or # not using the if conditional statement. if(len_str % num != 0): # If the statement is true, print "The given string cannot be divided into n equal halves". print("The given string cannot be divided into", num, "equal halves") else: # Else loop from 0 to length of the string with the step size of the number 'k' # using the for loop. print("The given string after dividing into", num, "equal halves:") for i in range(0, len_str, k): # Slice from the iterator value to the iterator +n value using slicing and # print them. print(gvn_str[i:i+k])

**Output:**

The given string after dividing into 5 equal halves: aaaa bbbb cccc dddd eeee

### Method #2: Using For Loop (User Input)

**Approach:**

- Give the string as user input using the input() function and store it in a variable.
- Give the number say ‘n’ as static input and store it in another variable.
- Calculate the len of the given string using the len() function and store it in another variable.
- Divide the length of the string by a given number and store it in another variable say ‘k’.
- Check if the length of the string modulus given number is not equal to ‘0’ or not using the if conditional statement.
- If the statement is true, print “The given string cannot be divided into n equal halves”.
- Else loop from 0 to length of the string with the step size of the number ‘k’ using the for loop.
- Slice from the iterator value to the iterator +k value using slicing and print them.
- The Exit of the Program.

**Below is the implementation:**

# Give the string as user input using the input() function and store it in a variable. gvn_str = input("Enter some random string = ") # Give the number say 'n' as user input using int(input()) and store it in another variable. num = int(input("Enter some random number = ")) # Calculate the len of the given string using the len() function and store it # in another variable. len_str = len(gvn_str) #Divide the length of the string by a given number and store it in another variable say 'k'. k=len_str//num # Check if the length of the string modulus given number is not equal to '0' or # not using the if conditional statement. if(len_str % num != 0): # If the statement is true, print "The given string cannot be divided into n equal halves". print("The given string cannot be divided into", num, "equal halves") else: # Else loop from 0 to length of the string with the step size of the number 'k' # using the for loop. print("The given string after dividing into", num, "equal halves:") for i in range(0, len_str, k): # Slice from the iterator value to the iterator +n value using slicing and # print them. print(gvn_str[i:i+k])

**Output:**

Enter some random string = 1234567890 Enter some random number = 2 The given string after dividing into 2 equal halves: 12345 67890

Explore more instances related to python concepts fromÂ Python Programming ExamplesÂ Guide and get promoted from beginner to professional programmer level in Python Programming Language.