Python | Split string in groups of n consecutive characters Last Updated : 23 Mar, 2023 Comments Improve Suggest changes Like Article Like Report 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 Python3 # Python code to split string # by every 3rd number # String initialization string = "Geeksforgeeks" # Defining splitting point n = 3 # Using list comprehension out = [(string[i:i+n]) for i in range(0, len(string), n)] # Printing output print(out) Method #2: Using zip_longest Python3 # Python code to split string of number # and character into every 4th number # Importing from itertools import zip_longest # Group function using zip_longest to split def group(n, iterable, fillvalue=None): args = [iter(iterable)] * n return zip_longest(fillvalue=fillvalue, *args) # String initialization str = '123GeeksForGeeks4567' # Split point n=4 # list of separated string out_string = [''.join(lis) for lis in group(n, str, '')] # Output list initialization out_no = [] # Converting list of string into list of integer for a in out_string: out_no.append(a) # Printing list print(out_no) Output['123G', 'eeks', 'ForG', 'eeks', '4567'] Method 3: Using a for loop and string slicing. Initialize an empty list to store the substrings.Define the splitting point, i.e., n=3.Create a for loop to iterate over the string.In each iteration, append the substring to the list using string slicing.Print the output list. Python3 # Python code to split string # by every 3rd number # String initialization string = "Geeksforgeeks" # Defining splitting point n = 3 # Using for loop and string slicing out = [] for i in range(0, len(string), n): out.append(string[i:i+n]) # Printing output print(out) Output['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. Comment More infoAdvertise with us Next Article Split String into List of characters in Python E everythingispossible Follow Improve Article Tags : Python Python Programs python-string Python string-programs Marketing +1 More Practice Tags : python Similar Reads Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t 2 min read Split String into List of characters in Python We are given a string and our task is to split this string into a list of its individual characters, this can happen when we want to analyze or manipulate each character separately. For example, if we have a string like this: 'gfg' then the output will be ['g', 'f', 'g'].Using ListThe simplest way t 2 min read Python - Equidistant consecutive characters Strings Given a Strings List, extract all the strings, whose consecutive characters are at the common difference in ASCII order. Input : test_list = ["abcd", "egil", "mpsv", "abd"] Output : ['abcd', 'mpsv'] Explanation : In mpsv, consecutive characters are at distance 3. Input : test_list = ["abcd", "egil", 9 min read Python Program to Split joined consecutive similar characters Given a String, our task is to write a Python program to split on the occurrence of a non-similar character. Input : test_str = 'ggggffggisssbbbeessssstt'Output : ['gggg', 'ff', 'gg', 'i', 'sss', 'bbb', 'ee', 'sssss', 'tt']Explanation : All similar consecutive characters are converted to separate st 5 min read Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli 2 min read Split String of list on K character in Python In this article, we will explore various methods to split string of list on K character in Python. The simplest way to do is by using a loop and split().Using Loop and split()In this method, we'll iterate through each word in the list using for loop and split it based on given K character using spli 2 min read Like