0

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

25th Oct 2016, 3:26 PM
Kelvin
Kelvin - avatar
7 Answers
+ 4
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".
25th Oct 2016, 4:12 PM
Zen
Zen - avatar
+ 3
@Kelvin: Yes, != is the operator to check if the two operands are not equal.
26th Oct 2016, 10:11 AM
Zen
Zen - avatar
+ 2
Zen is right. the third line you are asking is that very logic you need to implement to get the result.
25th Oct 2016, 5:01 PM
Tamoghna Saha
0
So is it safe to say that "!=" is exactly "not =="? etc. str[i] == '.' means that both of these operands are equivalent
26th Oct 2016, 4:57 AM
Kelvin
Kelvin - avatar
0
Ok it's all clear now. Thank you very much =)
26th Oct 2016, 10:22 AM
Kelvin
Kelvin - avatar
0
and you dont need to iterate i up to len(str) but up to len(str)-3
6th Nov 2016, 11:02 AM
Redstone Tehnik
Redstone Tehnik - avatar
0
hi
27th Nov 2019, 10:13 AM
Romeo
Romeo - avatar