#lec5examples.py

"""Practice writing functions that work with strings"""

def middle(text):
    """Returns: middle 3rd of text.
    Param text: a string with length divisible by 3"""
    size= len(text)
    start2= size//3
    start3= 2*size//3
    middle_third= text[start2:start3]
    return middle_third


def firstparens(text):
    """Returns: substring in first parenthesis
    Param text: a string with at least one ()"""
    start= text.index('(')
    end= text.index(')')
    inside= text[start+1:end]
    return inside


def firstparens_v2(text):
    """Returns: substring in first parenthesis
    Param text: a string with at least one ()"""
    # Find the open parenthesis
    start= text.index('(')
    # Store the part AFTER the open parenthesis
    substr= text[start+1:]
    # Find the close parenthesis
    end= substr.index(')')
    inside= substr[:end]
    return inside


def second(thelist):
    """Returns: the second word in a list of words separated by commas, with
    any leading or trailing spaces from the second word removed.
    Ex: second('A, B, C') --> 'B'
    Param thelist: a list of words with at least two commas"""
    start= thelist.index(',')
    tail= thelist[start+1:]
    end= tail.index(',')
    result= tail[:end].strip()
    return result
