Python/strings/split.py
Christian Clauss 9316e7c014
Set the Python file maximum line length to 88 characters (#2122)
* flake8 --max-line-length=88

* fixup! Format Python code with psf/black push

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
2020-06-16 10:09:19 +02:00

35 lines
866 B
Python

def split(string: str, separator: str = " ") -> list:
"""
Will split the string up into all the values separated by the separator
(defaults to spaces)
>>> split("apple#banana#cherry#orange",separator='#')
['apple', 'banana', 'cherry', 'orange']
>>> split("Hello there")
['Hello', 'there']
>>> split("11/22/63",separator = '/')
['11', '22', '63']
>>> split("12:43:39",separator = ":")
['12', '43', '39']
"""
split_words = []
last_index = 0
for index, char in enumerate(string):
if char == separator:
split_words.append(string[last_index:index])
last_index = index + 1
elif index + 1 == len(string):
split_words.append(string[last_index : index + 1])
return split_words
if __name__ == "__main__":
from doctest import testmod
testmod()