TheAlgorithms-Python
34 строки · 866.0 Байт
1def split(string: str, separator: str = " ") -> list:2"""3Will split the string up into all the values separated by the separator
4(defaults to spaces)
5
6>>> split("apple#banana#cherry#orange",separator='#')
7['apple', 'banana', 'cherry', 'orange']
8
9>>> split("Hello there")
10['Hello', 'there']
11
12>>> split("11/22/63",separator = '/')
13['11', '22', '63']
14
15>>> split("12:43:39",separator = ":")
16['12', '43', '39']
17"""
18
19split_words = []20
21last_index = 022for index, char in enumerate(string):23if char == separator:24split_words.append(string[last_index:index])25last_index = index + 126elif index + 1 == len(string):27split_words.append(string[last_index : index + 1])28return split_words29
30
31if __name__ == "__main__":32from doctest import testmod33
34testmod()35