Last active
November 10, 2023 11:27
-
-
Save stevensdotb/868a80782b4848957b76066840b8377c to your computer and use it in GitHub Desktop.
Long String Split
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def split_sring(*, string: str, max_chunk_length: int): | |
"""Split a string, preferible a long one into a list of chunks | |
parameters: | |
string -> String to be splited | |
max_chunk_length -> Chunks length | |
""" | |
chunks = [] | |
from_char = 0 | |
to_char = max_chunk_length | |
while True: | |
chunk = string[from_char:to_char] | |
remaining = string[from_char:] | |
if len(remaining) < max_chunk_length: | |
chunks.append(remaining) | |
break | |
chunks.append(chunk) | |
from_char = to_char | |
to_char = from_char + max_chunk_length | |
return chunks |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment