Python | Convert String to N chunks tuple Last Updated : 05 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report Sometimes, while working with Python Strings, we can have a problem in which we need to break a string to N sized chunks to a tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using list comprehension + tuple This is one approach in which this task can be performed. In this, we just iterate the String and break the chunks of string and construct the tuple using tuple() in one-liner. Python3 # Python3 code to demonstrate working of # Convert String to N chunks tuple # Using list comprehension + tuple() # initialize string test_string = "ggggffffgggg" # printing original string print("The original string : " + str(test_string)) # initialize N N = 4 # Convert String to N chunks tuple # Using list comprehension + tuple() res = tuple(test_string[i: i + N] for i in range(0, len(test_string), N)) # printing result print("Chunked String into tuple : " + str(res)) Output : The original string : ggggffffgggg Chunked String into tuple : ('gggg', 'ffff', 'gggg') Time Complexity: O(n), where n is the length of stringAuxiliary Space: O(n) Method #2 : Using zip() + iter() + join() + list comprehension The combination of above functions can also be used to perform this task. In this, we perform the act of making chunks using zip() + iter(). And cumulate the result using join(). Python3 # Python3 code to demonstrate working of # Convert String to N chunks tuple # Using zip() + iter() + join()+ list comprehension # initialize string test_string = "ggggffffgggg" # printing original string print("The original string : " + str(test_string)) # initialize N N = 4 # Convert String to N chunks tuple # Using zip() + iter() + join() + list comprehension res = tuple([''.join(ele) for ele in zip(*[iter(test_string)] * N)]) # printing result print("Chunked String into tuple : " + str(res)) Output : The original string : ggggffffgggg Chunked String into tuple : ('gggg', 'ffff', 'gggg') Time Complexity: O(n), where n is the length of the stringAuxiliary Space: O(n) Comment More infoAdvertise with us Next Article Python | Convert String to tuple list M manjeet_04 Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Convert tuple to string in Python The goal is to convert the elements of a tuple into a single string, with each element joined by a specific separator, such as a space or no separator at all. For example, in the tuple ('Learn', 'Python', 'Programming'), we aim to convert it into the string "Learn Python Programming". Let's explore 2 min read Convert String to Tuple - Python When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte 2 min read Python | Convert String to tuple list Sometimes, while working with Python strings, we can have a problem in which we receive a tuple, list in the comma-separated string format, and have to convert to the tuple list. Let's discuss certain ways in which this task can be performed. Method #1 : Using loop + split() + replace() This is a br 5 min read Python | Convert String to list of tuples Sometimes, while working with data, we can have a problem in which we have a string list of data and we need to convert the same to list of records. This kind of problem can come when we deal with a lot of string data. Let's discuss certain ways in which this task can be performed. Method #1: Using 8 min read Python - Convert Tuple String to Integer Tuple Interconversion of data is a popular problem developer generally deal with. One can face a problem to convert tuple string to integer tuple. Let's discuss certain ways in which this task can be performed. Method #1 : Using tuple() + int() + replace() + split() The combination of above methods can be 7 min read Python | Convert string tuples to list tuples Sometimes, while working with Python we can have a problem in which we have a list of records in form of tuples in stringified form and we desire to convert them to a list of tuples. This kind of problem can have its occurrence in the data science domain. Let's discuss certain ways in which this tas 4 min read Like