![]() |
VOOZH | about |
Given a string (be it either string of numbers or characters), write a Python program to split the string by every nth character.
Examples:
Input : str = "Geeksforgeeks", n = 3 Output : ['Gee', 'ksf', 'org', 'eek', 's'] Input : str = "1234567891234567", n = 4 Output : [1234, 5678, 9123, 4567]
Method #1: Using list comprehension
Method #2: Using zip_longest
['123G', 'eeks', 'ForG', 'eeks', '4567']
Method 3: Using a for loop and string slicing.
['Gee', 'ksf', 'org', 'eek', 's']
Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), as we are storing each substring in a list.