diff --git a/strings/split.py b/strings/split.py index ed194ec69c2f..2ee6d97c12d4 100644 --- a/strings/split.py +++ b/strings/split.py @@ -3,22 +3,38 @@ 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='#') + >>> split("apple#banana#cherry#orange", separator="#") ['apple', 'banana', 'cherry', 'orange'] >>> split("Hello there") ['Hello', 'there'] - >>> split("11/22/63",separator = '/') + >>> split("11/22/63", separator="/") ['11', '22', '63'] - >>> split("12:43:39",separator = ":") + >>> split("12:43:39", separator=":") ['12', '43', '39'] - >>> split(";abbb;;c;", separator=';') + >>> split(";abbb;;c;", separator=";") ['', 'abbb', '', 'c', ''] + + >>> split("apple--banana--cherry", separator="--") + Traceback (most recent call last): + ... + ValueError: separator should be a single character + + >>> split("apple", separator="") + Traceback (most recent call last): + ... + ValueError: separator should not be empty """ + if not separator: + raise ValueError("separator should not be empty") + + if len(separator) != 1: + raise ValueError("separator should be a single character") + split_words = [] last_index = 0