def extract_line(text, index):
 p = (text.rfind('\n', 0, index) + 1)
 q = text.find('\n', index)
 if (q < 0):
  q = len(text)
 return text[p:q]

def extract_line ( text , index ) : p = ( text . rfind ( '\n' , 0 , index ) + 1 ) q = text . find ( '\n' , index ) if ( q < 0 ) : q = len ( text ) return text [ p : q ]
text may be a multiline string; extract only the line containing the given character index .