Python Forum
short_str in long_str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
short_str in long_str
#6
(Jan-08-2018, 09:49 PM)Truman Wrote: 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'.

In order to get around that, you should split the string into a list of available options. The in operator can still be used at that point.
>>> can_read = "book"
>>> things = "website, blog, book"
>>> can_read_things = things.split(", ")
>>> can_read_things
['website', 'blog', 'book']
>>> can_read in can_read_things
True
>>> things = "website, blog, notebook"
>>> can_read_things = things.split(", ")
>>> can_read in can_read_things
False
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