Python Forum
Searching for parts of a string or word
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching for parts of a string or word
#1
Hello,

I´m trying to write a code create a little searching mashine. The code should be able to tell me by finding parts of a string or word that it is fond.

I have written the following code:
string1 = ["This is awesome", "awesome"]


def search(text, word):
    if text in string1 or word in string1:
        return "Word found"
    else:
        return "Word not found"


text = str(input())
word = str(input())
print(search(text, word))
it doesent work properly. If I type in "this is some tex" or "some" the code tells me that it doesent find anythink. But it should. Can someone help me with that? What did I miss. Thx
buran write Mar-14-2021, 03:47 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Reply
#2
Your code as currently written is looking at the list variable string1, which you have explicitly defined, to see if it contains either text or word. So, you are only going to get the "Word found" output if you enter "This is awesome" or "awesome", since those are the two values found in string1.

I'm not 100% sure of your expectations, but it sounds like maybe you want to search text to see if it contains word. You can do that by changing the first line of your search function.
Reply
#3
My code should react to my input. The input is compared to string1. If there are some similaritys, it should give me "Word found".
Reply
#4
Your code in it's current state does following:

if  "this is some tex" in ["This is awesome", "awesome"] or "some" in ["This is awesome", "awesome"]:
Quite obviously there is no match to be found.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#5
I think that this is closer to what you wanted your code to do:

string1 = ["This is awesome", "awesome"]
 
def search (text) :
	word_not_found = True
	word_list = text.split (' ')
	for word in word_list : 
		for phrase in string1 :
			if word in phrase :
				print (f'The word "{word}" was found.')
				word_not_found = False
	if word_not_found :
		print ('No matches were found.')

search (input ('Enter some text : '))
Ulumulu likes this post
Reply
#6
(Mar-14-2021, 05:14 PM)Ulumulu Wrote: My code should react to my input. The input is compared to string1. If there are some similaritys, it should give me "Word found".

What does "some similarities" mean? If you enter a string of random non-letter characters that also includes the letter "t", would that be a match since there is a t in one of the values in string1? You need to be exact in defining what does or does not constitute a match.

You also need to keep in mind that your variable string1 is a list, not a string. Your inputs are string values (and would be even without str(), which serves no purpose since input() returns a string by default). You are only going to find a string within a list if one of the values in the list exactly matches the string. If you want to search within each string in the list, you need to iterate through the list.
Ulumulu likes this post
Reply
#7
"in" works differently if you are comparing strings than if you are comparing a string to a list.

You are doing this:
"some" in ["This is awesome", "awesome"]
Used this way, "in" returns True if "some" == "This is awesome" or "some" == "awesome". Obviously neither is a match, so your code finds no match

The comparison is different if you use "in" to compare two strings.
"some" in "This is awesome"
This evaluates True because when comparing strings, "in" is looking for a substring in "This is awesome" that matches "some".

Neither of these comparisons are True if you enter "this is some tex". This is not a substring of "This is awesome" or "some".

I think what you want to do is break everything into words and compare the two sets of words. You could do this:
words  = "This is an awsome thing".split()

def search(text, words):
    """Return True if any word in text matches any part of a word in words"""
    for text in text.split():
        for word in words:
            if text in word:
                print(text, word)

search("This is a test of some thing", words)
This prints all the matching words which is more interesting than match/no match.

Another thing you can do is search for the longest common substring between to strings. This is one of those common homework problems that you can find solved a million times online.
Ulumulu likes this post
Reply
#8
There is also str.count method:

>>> help(str.count)
Help on method_descriptor:

count(...)
    S.count(sub[, start[, end]]) -> int

    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.

>>> 'abrakadabra'.count('abra')
2
>>> 'abrakadabra'.count('some')
0
Ulumulu likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
Thx gyus, I really aprecciate it.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Exclamation A function that takes a word and a string of forbidden letters pooyan89 5 4,576 Jun-12-2019, 09:44 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

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