I was wondering how you define a funtion which a phrase and two words are inputted and the output is the part of the prhase between the two words. Do you convert the string to a list?? I would apprecitate any help.

Yes, I'd probably start breaking the sentence into a list of words. Two problems come to mind: (1) Punctuation would need to be trimmed from the end of each word (but possibly need to be remembered in case the punctuated word(s) are part of the output); (2) Individual words can appear multiple times so some disambiguation might be required.

For example, the string "We'll meet again, don't know where, don't know when." readily splits into ["We'll", 'meet', 'again,', "don't", 'know', 'where,', "don't", 'know', 'when.'] . What do you do for the input words "don't" and "when"?

How would you split the string like that?
I know it can be ambiguious, and if there are two words that are the same I need the longest phrase out of the two. In your case the output should be "know where, don't"

Howdy,
Okay, I'm brand new to DaniWeb, and new to programming myself. And, as seeing as I'm not quite sure what you're asking, I'll post what I think you're wanting. If not, I'm sorry and hope something here might help you anyway.
I apologize for the bad format of my coding, as I said, I'm rather new to this.

#Funtion to return all words of a string except the first and last words.
def midphrase(input1):
      input1 = input1.split()
      return input1[1:-1][/INDENT]

#Ask for input
input0 = raw_input("Please type a sentence: ")

#Call to function
input0 = midphrase(input0)

#Default Variables for reasembly
count = 0
output1 = ""

#Reasemble the returned string
for word in input0:
      output1 = output1 + " " + input0[count]
      count = count + 1[/INDENT]

print output1

Okay, I see I was totally on the wrong track of what you were asking. Sorry to have wasted your time. :)

can you give a living example?
For me I don't understand what you are exactly saying! I don't want to redefine it

Howdy,
Okay, I'm brand new to DaniWeb, and new to programming myself.......... I apologize for the bad format of my coding, as I said, I'm rather new to this.

Welcome Andy!
Don't worry about being new, you learned to speak your language, right? You will learn Python too :)

Just use "

put your code here

" and you will come out with nice pycode!
Happy Pyprogramming!!
Steve

Thanks Steve!
It's nice to know you're welcome somewhere. I'll do my best to learn Python, and contribute here whenever my, hopefully, ever growing knowledge is needed.

Andy

I posted to a similar thread recently, but I can't remember where. The following will preserve punctuation:

from string import punctuation as stuff

def words_between(s, first, second):
    # return the words between first and second words
    words = [word.lower() for word in s.split()]
    # catch error if word not in sentence
    try:
        # strip punctuation for matching
        idx1 = [word.strip(stuff) for word in words].index(first.lower())
        # start search for second word after idx1
        idx2 = [word.strip(stuff) \
                for word in words].index(second.lower(),idx1+1)
        return words[idx1+1:idx2]
    except ValueError, e:
        return "ValueError: %s" % e

sentence = "The small country was ruled by a truculent dictator."
print words_between(sentence, "was", "dictator")
print words_between(sentence, "was", "the")

sentence = "The children were delighted to find that the costume trunk was replete with dresses, hats, capes, and all sorts of props to play make-believe."
print words_between(sentence, "the", "the")
print ' '.join(words_between(sentence, "was", "capes"))

Output:

>>> ['ruled', 'by', 'a', 'truculent']
ValueError: list.index(x): x not in list
['children', 'were', 'delighted', 'to', 'find', 'that']
replete with dresses, hats,
>>>

Okay, I see this has already been answered, but I just spent nearly two hours myself trying to figure it out. lol.... two hours.

Well, I'm posting it anyway. Please forgive the childlike approach I've taken, it's the only way I can do it for now.

def FindFirst(input1, input2):
    input2 = input2.split()
    count = 0
    for word in input2:
        if input1 == input2[count]:
            return count
        else:
            count += 1
def FindLast(input1, input2):
    input2 = input2.split()
    count = -1
    for word in input2:
        if input1 == input2[count]:
            return count
        else:
            count -= 1

#Ask for input
input0 = raw_input("Please type a sentence: ")
inputFirst = raw_input("Please type the first word to find: ")
inputLast = raw_input("Please type the last word to find: ")
                      
#Call to function
x = FindFirst(inputFirst, input0)
x += 1
y = FindLast(inputLast, input0)
input0 = input0.split()
input0 = input0[x:y]

#Default Variables for reasembly
count = 0
output1 = ""

#Reasemble the returned string
for word in input0:
    output1 = output1 + " " + input0[count]
    count = count + 1

print output1

Andy,

No forgiveness necessary. Doing exercises like this is a good way to learn. That's one reason I am doing it.

I'm surprised that nobody suggests to use the re module, like this

import re

sentence = raw_input("Please type a sentence: ")
first = raw_input("Please type the first word to find: ")
last = raw_input("Please type the last word to find: ")

pattern = re.compile(
    "%s(.*)%s" % (re.escape(first), re.escape(last)),
    re.DOTALL
    )

match = pattern.search(sentence)
if match is None:
    print "The sentence does not contain '%s...%s'" %(first, last)
else:
    print "found: %s" % match.group(1)

The re module is a very powerful tool for searching and modifying strings.

Grib,
I'm too Jr. in py that I have no Idea of that module. For Now I try to familiarize with Wxpython while learning python extensively. May be some time later I will dig it. BTW what does it do in summary??

What the re module gives you is 1) A language, the regular expression language which can express patterns like "the word foo followed by any sequence of characters followed by the word bar", and more complex patterns, 2) A compiler which understands such a pattern and produces optmized code for searching the pattern in a string. This code is contained in the regular expression objects, 3) Methods to use this code for searching the occurrences of the given pattern in any string, and also to modify the string according to arbitrary rules that you would define when the pattern is found, etc

Wow thanks to everyone. I should be able to do it now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.