loops - Python 3 - Check if finding index returns an error -
i'm trying iterate through code create find() function patterns. want check if string[index + 1] returns indexerror (slash if out of bounds) in order know whether keep iterating through string or return index of first occurence. i've tried couple different things, since it's in class created keep getting attribute error. here couple things have tried:
with self.assertraises(indexerror): p[ind + 1] print(ind - len(p)) if not p[ind + 1]:
you can use len()
function.
if 0 <= ind < len(p): ... # p[ind] exist
len()
returns length of string, greatest index in string len()-1
(because indexes counted 0
). may don't want use negative index because elements end (for example, 'abc'[-1]
return 'c'
.
Comments
Post a Comment