Hackbright Code Challenges

Split a String

Split a String

Whiteboard

Medium

Challenge

Easier

Concepts

Loops, Strings

Download

split.zip

Solution

Split a String: Solution


In this challenge, you should create a function that works like that built-in Python .split() method 1.

You may not use the split method in your solution!

You may not use regular expressions in your solution!

For example:

>>> split("i love balloonicorn", " ")
['i', 'love', 'balloonicorn']

>>> split("that is which is that which is that", " that ")
['that is which is', 'which is that']

>>> split("that is which is that which is that", "that")
['', ' is which is ', ' which is ', '']

>>> split("hello world", "nope")
['hello world']

We’ve provided a function in split.py with tests, but it’s not implemented yet:

def split(astring, splitter):
    """Split a string by splitter and return list of splits."""
1

Note: the actual Python split method has special behavior when it is not passed anything for the splitter – you do not need to implement that.