Suppose we have a string s and an integer n, we have to split the s into n-sized pieces.
So, if the input is like s = "abcdefghijklmn", n = 4, then the output will be ['abcd', 'efgh', 'ijkl', 'mn']
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
class Solution: def solve(self, s, n): i=0 f= while(i<len(s)): f.append(s[i:i+n]) i+=n return(f) ob = Solution() print(ob.solve("abcdefghijklmn", 4))
['abcd', 'efgh', 'ijkl', 'mn']