def to_doctests(code, first='>>> ', cont='... '):
    """Add docstring prompts to code snippets"""
    new_code = []
    code_list = code.split('\n')
    prefix = first
    last_line_no = len(code_list) - 1
    for i, line in enumerate(code_list):
        if line.strip() != '':
            prefix = cont if line.startswith(' ') else first
            new_code.append(prefix + line)
            continue
        # For blank lines, we always strip the whitespace, but we will need a
        # prefix for anything but the last line
        if i == last_line_no:
            prefix = ''
        elif code_list[i + 1].startswith(' '):
            # Use continuation for line following
            prefix = cont.rstrip()
        else:  # Use prefix for previous line
            prefix = prefix.rstrip()
        new_code.append(prefix)

    return '\n'.join(new_code)