(Jan-08-2018, 06:28 AM)squenson Wrote: This is correct! The string "book" is found at position 0 in the string "book notebook textbook". In python, positions of lists, strings and tuples start with 0. Try with an example where the string is NOT found, what returned value do you get? Now how can you transform these results in the values True or False?
I receive value 4.
(Jan-08-2018, 09:46 AM)buran Wrote: Although you can usefind()
method of str to search if one string is present in another, it is better to usein
>>> can_read = 'book' >>> can_read_things = 'website, blog, book' >>> can_read_things.find(can_read) 15 >>> can_read2 = 'newspaper' >>> can_read_things.find(can_read2) -1 >>> can_read in can_read_things True >>> can_read2 in can_read_things False >>>As you can see, when the string is not present, find() returns -1, so you can make the conclusion.
However usingin
, returnsTrue
orFalse
Thank you. Still, a new problem arises. If I add 'book' in the first variable and 'notebook' in the second it will give True although book and notebook are different things. The reason is that last 4 characters in 'notebook' match with 'book'.