Sometimes, while working with Python strings, we can have problem in which we need to find location of a particular word. This can have application in domains such as day-day programming. Lets discuss certain ways in which this task can be done.
Method #1: Using re.findall() + index() This is one of the way in which we can find the location where word exists. In this we look for substring pattern using findall() and its position using index().
Python3
# Python3 code to demonstrate working of # Word location in String # Using findall() + index() import re # initializing string test_str = 'neveropen is best for Lazyroar' # printing original string print ( "The original string is : " + test_str) # initializing word wrd = 'best' # Word location in String # Using findall() + index() test_str = test_str.split() res = - 1 for idx in test_str: if len (re.findall(wrd, idx)) > 0 : res = test_str.index(idx) + 1 # printing result print ( "The location of word is : " + str (res)) |
The original string is : neveropen is best for Lazyroar The location of word is : 3
Time Complexity: O(n), where n is the length of the input string test_str.
Auxiliary Space: O(n), because we are splitting the input string into a list of words, which takes up O(n) space.
Method #2: Using re.sub() + index() This performs task in similar way as above method. In this also regex is employed. We use different regex function in this method.
Python3
# Python3 code to demonstrate working of # Word location in String # Using re.sub() + index() import re # initializing string test_str = 'neveropen is best for Lazyroar' # printing original string print ( "The original string is : " + test_str) # initializing word wrd = 'best' # Word location in String # Using re.sub() + index() res = re.sub( "[^\w]" , " " , test_str).split() res = res.index(wrd) + 1 # printing result print ( "The location of word is : " + str (res)) |
The original string is : neveropen is best for Lazyroar The location of word is : 3
Time Complexity: O(n)
Auxiliary Space : O(n)
Method #3: Using split() and index() methods
Python3
# Python3 code to demonstrate working of # Word location in String # initializing string test_str = 'neveropen is best for Lazyroar' # printing original string print ( "The original string is : " + test_str) # initializing word wrd = 'best' x = test_str.split() res = x.index(wrd) + 1 # printing result print ( "The location of word is : " + str (res)) |
The original string is : neveropen is best for Lazyroar The location of word is : 3
Time Complexity: O(n)
Auxiliary Space : O(n)
Method #4: Using in operator:
Python3
test_str = 'neveropen is best for Lazyroar' wrd = 'best' # printing original string print ( "The original string is : " + test_str) if wrd in test_str.split(): res = test_str.split().index(wrd) + 1 else : res = - 1 print ( "The location of word is : " + str (res)) #This code is contributed by Jyothi pinjala |
The original string is : neveropen is best for Lazyroar The location of word is : 3
Time Complexity: O(n)
Auxiliary Space : O(n)
Method 5: Using the split() method and a list comprehension:
This approach splits the test string into words using the split() method, creates a list of indices where the word appears using a list comprehension, and adds 1 to each index to account for 0-based indexing. If the list of indices is not empty, it sets the result to the first index in the list. Otherwise, it sets the result to -1.
Python3
# Set a test string and the word to be searched test_str = 'neveropen is best for Lazyroar' wrd = 'best' # Split the test string into words, create a list of indices where the word appears, and add 1 to each index (to account for 0-based indexing) indices = [i + 1 for i, word in enumerate (test_str.split()) if word = = wrd] # If the list of indices is not empty, set the result to the first index in the list # Otherwise, set the result to -1 if indices: res = indices[ 0 ] else : res = - 1 # Print the result print ( "The location of word is : " + str (res)) |
The location of word is : 3
Time complexity: O(n), where n is the length of the test string (due to the need to split the string and create a list of indices using a list comprehension)
Auxiliary space: O(n) (since the method creates a list of indices)
Method #6: Using a loop to iterate over the words in the string
Split the test string into words, and then iterate over the words to find the index of the first occurrence of the word we are searching for. If we find the word, we can return the index (plus 1 to account for 0-based indexing). If we reach the end of the loop without finding the word, we can return -1.
Python3
# Set a test string and the word to be searched test_str = 'neveropen is best for Lazyroar' wrd = 'best' # Split the test string into words words = test_str.split() # Iterate over the words to find the index of the first occurrence of the word we are searching for for i, word in enumerate (words): if word = = wrd: res = i + 1 break else : res = - 1 # Print the result print ( "The location of word is : " + str (res)) |
The location of word is : 3
Time complexity: O(n), where n is the number of words in the test string, because we need to iterate over each word in the string.
Auxiliary space: O(1), because we only need to store the variables used in the loop.