Hackbright Code Challenges

Split a String: Solution

Split a String: Solution

Problem

Split a String

Whiteboard

Medium

Challenge

Easier

Concepts

Loops, Strings

Download

split-solution.zip


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

    # START SOLUTION

    out = []
    index = 0

    while index <= len(astring):

        curr_index = index
        index = astring.find(splitter, index)

        if index != -1:
            out.append(astring[curr_index:index])
            index += len(splitter)

        else:
            # couldn't find any more instances of splitter in astring
            out.append(astring[curr_index:])
            break

    return out