# Split String of Size N in Python

PythonServer Side ProgrammingProgramming

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 −

• i:= 0
• f:= a new list
• while i < size of s, do
• insert s[from index i to i+n-1] at the end of f
• i := i + n
• return f

Let us see the following implementation to get better understanding −

## Example

Live Demo

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))

## Input

"abcdefghijklmn", 4

## Output

['abcd', 'efgh', 'ijkl', 'mn']
{"ft_lang_label":"__label__en","ft_lang_prob":0.65654826,"math_prob":0.95221066,"size":514,"snap":"2021-43-2021-49","text_gpt3_token_len":150,"char_repetition_ratio":0.11176471,"word_repetition_ratio":0.0,"special_character_ratio":0.3229572,"punctuation_ratio":0.18965517,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99811107,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-10-21T05:08:07Z\",\"WARC-Record-ID\":\"<urn:uuid:efdd7e0d-53e4-45bf-91fb-09e4b78a268e>\",\"Content-Length\":\"30901\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:ae0ffbc7-df8b-403a-84ee-5d0b786be391>\",\"WARC-Concurrent-To\":\"<urn:uuid:1fe5b6de-9e94-41bc-ba55-3504de83cc72>\",\"WARC-IP-Address\":\"72.21.91.42\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/split-string-of-size-n-in-python\",\"WARC-Payload-Digest\":\"sha1:FQIMXP64K7XTNCUKQHX4LNNLUYT3OUTC\",\"WARC-Block-Digest\":\"sha1:YRVDELYMBJKIRFNMX2SZ7GLQN6Q4T5F6\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-43/CC-MAIN-2021-43_segments_1634323585381.88_warc_CC-MAIN-20211021040342-20211021070342-00080.warc.gz\"}"}