Find the index of the end of a word in python

python find
python string find all
python find last occurrence in string
python rfind
python last index of
python index
difference between find and rfind in python
python find pattern in string

Is there a nice(er) way to find the end index of a word in a string?

My method is like that:

text = "fed up of seeing perfect fashion photographs"
word = "fashion"
wordEndIndex = text.index(word) + len(word) - 1

I cannot comment on whether this is a better way, but an alternative to what you have suggested would be to find the next space after that word and use that to get the index.

text = "fed up of seeing perfect fashion photographs"
word = "fashion"
temp = text.index(word)
wordEndIndex = temp + text[temp:].index(' ') - 1

Your approach seems more natural, and is possibly faster too.

Python String rindex() Method, Python string method rindex() returns the last index where the substring str is found, or raises an exception if no such index exists, optionally restricting the search to string[beg:end]. The index () method finds the first occurrence of the specified value. The index () method raises an exception if the value is not found. The index () method is almost the same as the find () method, the only difference is that the find () method returns -1 if the value is not found. (See example below) string .index ( value, start, end )

It depends whether you really want to know the end index or not. Presumably you're actually more interested in the bits of the text after that? Are you then doing something like this?

>>> text[wordEndIndex:]
'n photographs'

If you really do need the index, then do what you've done, but wrap it inside a function that you can call for different texts and words so you don't have to repeat this code. Then it's simple and understandable, if you give the function a descriptive name.

On the other hand, if you're more interested in the bits of text, then don't even bother working out what the index is:

>>> text.split(word)
['fed up of seeing perfect ', ' photographs']

Of course this will get more complicated if the word can appear more than once in the text. In that case maybe you could define a different function to split on the first occurrence of the word and just give back the before and after components, without ever returning any numerical indexes.

Python String rfind() Method, Python string method rfind() returns the last index where the substring str is found, or -1 if no such index exists, optionally restricting the search to string[beg:end]. The Python string find method – search in strings The find() function returns the index number of the first occurrence of the given search term in the specified string. If the specified string does not contain the search term, the find() returns -1.

Just for fun, here's a first–principles version that finds the index of the last character of the word in a single pass:

def word_end_index(text, word):
    wi = wl = len(word)
    for ti, tc in enumerate(text):
        wi = wi - 1 if tc == word[-wi] else wl
        if not wi:
            return ti

    return -1

I had some shorter versions but they used slices which would duplicate strings all over the place which is rather inefficient.

Python String, end : Ending position where suffix is needs to be checked within the string. the first repeated word in a string in Python using Dictionary · Python | Find Mixed  The index () method returns the index of a substring inside the string (if found). If the substring is not found, it raises an exception. The syntax of index () method for string is: str.index (sub [, start [, end]] ) index () Parameters. The index () method takes three parameters: sub - substring to be searched in the string str.

Python String index(), Python string find, python find string in string, python find word in string, Python index in the string where substring “sub” is found within the slice s[start:end]. Python List index() The index() method searches an element in the list and returns its index. In simple terms, index() method finds the given element in a list and returns its position. However, if the same element is present more than once, index() method returns its smallest/first position.

Python String find(), Where in the text is the word "welcome"? The index() method is almost the same as the find() method, the only difference is Default is to the end of the string  sub : It’s the substring which needs to be searched in the given string. start : Starting position where sub is needs to be checked within the string. end : Ending position where suffix is needs to be checked within the string. NOTE : If start and end indexes are not provided then by default it takes 0

Python String index() Method, How do you find the last index of a string in Python? Definition and Usage. The find() method finds the first occurrence of the specified value. The find() method returns -1 if the value is not found. The find() method is almost the same as the index() method, the only difference is that the index() method raises an exception if the value is not found.

Comments
  • What you have looks fine to me. Depending on what you are using that information for, there might be better ways to achieve the end goal. Perhaps post some more details about what you're really trying to do?
  • re.search(word, text).end() but it may not be faster than your method
  • re.search(word, text).end() - 1 gives the same answer as what you have in wordEndIndex
  • Using regex is quite drastically slower, (about 3.5 times) so unless your situation benefits in some other way by using regex, it's probably a better idea to keep on with what you have