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 = '/')
(string: str, separator: str = " ")
| 1 | def split(string: str, separator: str = " ") -> list: |
| 2 | """ |
| 3 | Will 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 | >>> split(";abbb;;c;", separator=';') |
| 19 | ['', 'abbb', '', 'c', ''] |
| 20 | """ |
| 21 | |
| 22 | split_words = [] |
| 23 | |
| 24 | last_index = 0 |
| 25 | for index, char in enumerate(string): |
| 26 | if char == separator: |
| 27 | split_words.append(string[last_index:index]) |
| 28 | last_index = index + 1 |
| 29 | if index + 1 == len(string): |
| 30 | split_words.append(string[last_index : index + 1]) |
| 31 | return split_words |
| 32 | |
| 33 | |
| 34 | if __name__ == "__main__": |