Python Forum
short_str in long_str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
short_str in long_str
#4
(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. Think

(Jan-08-2018, 09:46 AM)buran Wrote: Although you can use find() method of str to search if one string is present in another, it is better to use in

>>> 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 using in, returns True or False

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'.
Reply


Messages In This Thread
short_str in long_str - by Truman - Jan-08-2018, 12:46 AM
RE: short_str in long_str - by squenson - Jan-08-2018, 06:28 AM
RE: short_str in long_str - by Truman - Jan-08-2018, 09:49 PM
RE: short_str in long_str - by buran - Jan-08-2018, 09:46 AM
RE: short_str in long_str - by buran - Jan-08-2018, 10:08 PM
RE: short_str in long_str - by nilamo - Jan-08-2018, 10:12 PM
RE: short_str in long_str - by Truman - Jan-08-2018, 10:31 PM
RE: short_str in long_str - by squenson - Jan-08-2018, 10:49 PM
RE: short_str in long_str - by Gribouillis - Jan-08-2018, 11:38 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020