Return True if the given string contains an appearance of "xyz" where the xyz is not directly preceeded by a period(.). So "xxyz" is True but "x.xyz" is False.

One of the many correct answers is: def xyz_there(str): for i in range(len(str)): if str[i] != '.' and str[i+1:i+4] == 'xyz': return True if str[0:3] == "xyz": return True return False Can anyone explain what the line of code means on line 3? Thanks in advance. Source of qn: codingbat.com

10/25/2016 3:26:29 PM


7 Answers

New Answer


str[i] != '.' checks if the character at index i is not a dot. str[i+1:i+4] == 'xyz' checks if the substring from indexes i+1 to i+3 is "xyz".


@Kelvin: Yes, != is the operator to check if the two operands are not equal.


Zen is right. the third line you are asking is that very logic you need to implement to get the result.


So is it safe to say that "!=" is exactly "not =="? etc. str[i] == '.' means that both of these operands are equivalent


Ok it's all clear now. Thank you very much =)


and you dont need to iterate i up to len(str) but up to len(str)-3