{"id":17305,"date":"2021-08-26T10:42:45","date_gmt":"2021-08-26T05:12:45","guid":{"rendered":"https:\/\/python-programs.com\/?p=17305"},"modified":"2021-11-22T18:37:11","modified_gmt":"2021-11-22T13:07:11","slug":"python-program-to-divide-a-string-in-n-equal-parts","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-divide-a-string-in-n-equal-parts\/","title":{"rendered":"Python Program to Divide a String in ‘N’ Equal Parts"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Evil Number or Not<\/a> Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to divide the given string into “N” equal parts.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explore more instances related to python concepts from\u00a0Python Programming Examples<\/a>\u00a0Guide and get promoted from beginner to professional programmer level in Python Programming Language.<\/p>\n 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: …<\/p>\n
\nGiven a string and the task is to divide the given string into “N” equal parts.<\/p>\nGiven string = \"aaaabbbbccccddddeeee\"<\/pre>\n
The given string after dividing into 5 equal halves:\r\naaaa\r\nbbbb\r\ncccc\r\ndddd\r\neeee<\/pre>\n
Given string = \"hellobtechgeeks\"<\/pre>\n
The given string cannot be divided into 4 equal halves<\/pre>\n
Program to Divide a String in ‘N’ Equal Parts<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = \"aaaabbbbccccddddeeee\"\r\n# Give the number say 'n' as static input and store it in another variable.\r\nnum = 5\r\n# Calculate the len of the given string using the len() function and store it\r\n# in another variable.\r\nlen_str = len(gvn_str)\r\n# Divide the length of the string by a given number and store it in another variable say 'k'.\r\nk = len_str\/\/num\r\n# Check if the length of the string modulus given number is not equal to '0' or\r\n# not using the if conditional statement.\r\nif(len_str % num != 0):\r\n # If the statement is true, print \"The given string cannot be divided into n equal halves\".\r\n print(\"The given string cannot be divided into\", num, \"equal halves\")\r\nelse:\r\n # Else loop from 0 to length of the string with the step size of the number 'k'\r\n # using the for loop.\r\n print(\"The given string after dividing into\", num, \"equal halves:\")\r\n for i in range(0, len_str, k):\r\n # Slice from the iterator value to the iterator +n value using slicing and\r\n # print them.\r\n print(gvn_str[i:i+k])\r\n<\/pre>\n
The given string after dividing into 5 equal halves:\r\naaaa\r\nbbbb\r\ncccc\r\ndddd\r\neeee<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the string as user input using the input() function and store it in a variable.\r\ngvn_str = input(\"Enter some random string = \")\r\n# Give the number say 'n' as user input using int(input()) and store it in another variable.\r\nnum = int(input(\"Enter some random number = \"))\r\n# Calculate the len of the given string using the len() function and store it\r\n# in another variable.\r\nlen_str = len(gvn_str)\r\n#Divide the length of the string by a given number and store it in another variable say 'k'.\r\nk=len_str\/\/num\r\n# Check if the length of the string modulus given number is not equal to '0' or\r\n# not using the if conditional statement.\r\nif(len_str % num != 0):\r\n # If the statement is true, print \"The given string cannot be divided into n equal halves\".\r\n print(\"The given string cannot be divided into\", num, \"equal halves\")\r\nelse:\r\n # Else loop from 0 to length of the string with the step size of the number 'k'\r\n # using the for loop.\r\n print(\"The given string after dividing into\", num, \"equal halves:\")\r\n for i in range(0, len_str, k):\r\n # Slice from the iterator value to the iterator +n value using slicing and\r\n # print them.\r\n print(gvn_str[i:i+k])\r\n<\/pre>\n
Enter some random string = 1234567890\r\nEnter some random number = 2\r\nThe given string after dividing into 2 equal halves:\r\n12345\r\n67890<\/pre>\n
\n