kikucik89
kikucik89

Reputation: 17

How to get certain phrase from the middle of the string in Python

I have silly question - How can I get just the middle bunch of words? I'm missing with one value.

string = "Here is the content and I want to get middle words"
start = string.index("content")
print(string[start:i'm stucked here!!!])

Any ideas how to count this value to delete the things after content?

Upvotes: 0

Views: 4823

Answers (5)

NL23codes
NL23codes

Reputation: 1209

Not completely knowing how versatile you need this to be, or the full purpose of its use, here are a few options.

You could take the string and convert it to a list. This would give you more iteration options, depending on what your needs would be:

string = "Here is the content and I want to get middle words"
_string = string.split()
print(_string)

output = ['Here', 'is', 'the', 'content', 'and', 'I', 'want', 'to', 'get', 'middle', 'words']

If you wanted a method where you could change the word you're searching for and it would not throw an exception if the word wasn't in the phrase, here's a possibility. It may look a little verbose but again, not knowing what application you'd be using it for, the 'safety nets' it has could be helpful:

_string = "Here is the content and I want to get middle words"
word_find = "content"

def get_word_indicies(input_string, word_find):
    start_index = None
    end_index = None

    for i, x in enumerate(input_string):
        if x in word_find:
            if start_index is None:
                start_index = I
            else:
                end_index = i + 1
                if (end_index - start_index) == len(word_find):
                    return [start_index, end_index]
        else:
            start_index = None
            end_index = None
    start_index = None
    end_index = None
    return [start_index,end_index]

x, y = get_word_indicies(_string, word_find)
print(_string[x:y])

Upvotes: 0

Nick Skywalker
Nick Skywalker

Reputation: 1087

The syntax string[start:end] return the sub-sequence of string from index start to end (not included).

If you want to extract a word that start at the position start and that word is of length n, then the string[start:(start+n)] will return the word.

To get the length of a word, use len(word).

That gives you:

string = "Here is the content and I want to get middle words"
word = "content"
start = string.index(word)
end = start + len(word)
print(string[start:end])

Upvotes: 1

Jan
Jan

Reputation: 227

How about this?

string = "Here is the content and I want to get middle words"
word = "content"
start = string.index(word)

print(string[start:start+len(word)])

Upvotes: 0

roy05
roy05

Reputation: 431

str.search(searchItem) will return the start index of the term you are looking for and -1 if it doesn't exist.

So in your case,

string.search("content") will give you 12, which is the index of 'c' in content;

so,

start = string.search("content") + len("content") //len to add the search term as well

//and to get substring:

string = string.substr(0,start);

Upvotes: 0

Jis Mathew
Jis Mathew

Reputation: 175

For the specific use case you can use the below code

string = "Here is the content and I want to get middle words"
start = string.index("content") + len("content")
string = string [:start]
print(string)

Upvotes: 1

Related Questions