def find_line(string, index):
    """For the index into string, return the line number (0-based)
    character within line (0-based), and the line itself (no newline)."""
    line_start_index = string.rfind('\n',0,index)+1
    line_end_index = string.find('\n',index)
    if line_end_index < 0:
        line_end_index = len(string)
    lineindex = string.count('\n',0,index)
    line = string[line_start_index:line_end_index]
    charindex = index - line_start_index
    return (lineindex, charindex, line)