Python Forum
short_str in long_str
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
short_str in long_str
#1
I'm following tutorial and one of the problems to solve is this one:
1. [ ] get user input for a single word describing something that can be read save in a variable called can_read
e.g. - "website", "newspaper", "blog", "textbook"
2. [ ] get user input for 3 things can be read
save in a variable called can_read_things
3. [ ] print true if the can_read string is found
in the can_read_things string variable

can_read = input("add1: ")
can_read_things = input("add3: ")
print(can_read_things.find(can_read))
I wrote this code with add1 = book and add3 = book notebook textbook but when I run the code receive outcome 0. Help is appreciated.
Reply
#2
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?

Also, consider the case: add1 is "book" and add3 is "notebook newspaper postit". Is the answer correct?

Good luck!
Reply
#3
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
Reply
#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
#5
you need to convert the user input into list, one way or another - e.g. split the long string, or construct the list by asking user for input one item at a time...
Reply
#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
#7
Thank you, that was very useful. One more question - is there a simple way to make input case-insensitive? For example, if first variable is 'book' and the second has 'Book' - want that to be a match. Did some google search but found only complicated solutions.
Reply
#8
Try googling "python string lowercase"
Reply
#9
(Jan-08-2018, 10:31 PM)Truman Wrote: if first variable is 'book' and the second has 'Book' - want that to be a match
You only need to convert all user input to lowercase.
Reply


Forum Jump:

User Panel Messages

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