In [10]:
ls_result = '''/bin
/boot
/dev
/etc
/home
/lib
/lost+found
/mnt
/opt
/proc
/root
/run
/sbin
/srv
/sys
/tmp
/usr
/var'''

In [11]:
def grep(text, word, exclude=False):
    '''
    Affiche les lignes de text qui contiennent word.
    
    Si exclude est True, n'affiche pas les lignes qui contiennent word.
    '''
    for line in text.split():
        if word in line and not exclude:
            print(line)
        elif word not in line and exclude:
            print(line)

In [12]:
grep(ls_result, 'a')
# doit n'afficher que /var


/var

In [13]:
grep(ls_result, 't', exclude=True)
# doit afficher
# /bin
# /dev
# /home
# /lib
# /proc
# /run
# /sbin
# /srv
# /sys
# /usr
# /var


/bin
/dev
/home
/lib
/proc
/run
/sbin
/srv
/sys
/usr
/var