Python | Word location in String
Last Updated :
09 Mar, 2023
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
import re
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + test_str)
wrd = 'best'
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
print ( "The location of word is : " + str (res))
|
Output :
The original string is : geeksforgeeks is best for geeks
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
import re
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + test_str)
wrd = 'best'
res = re.sub( "[^\w]" , " " , test_str).split()
res = res.index(wrd) + 1
print ( "The location of word is : " + str (res))
|
Output :
The original string is : geeksforgeeks is best for geeks
The location of word is : 3
Time Complexity: O(n)
Auxiliary Space : O(n)
Method #3: Using split() and index() methods
Python3
test_str = 'geeksforgeeks is best for geeks'
print ( "The original string is : " + test_str)
wrd = 'best'
x = test_str.split()
res = x.index(wrd) + 1
print ( "The location of word is : " + str (res))
|
Output
The original string is : geeksforgeeks is best for geeks
The location of word is : 3
Time Complexity: O(n)
Auxiliary Space : O(n)
Method #4: Using in operator:
Python3
test_str = 'geeksforgeeks is best for geeks'
wrd = 'best'
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))
|
Output
The original string is : geeksforgeeks is best for geeks
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
test_str = 'geeksforgeeks is best for geeks'
wrd = 'best'
indices = [i + 1 for i, word in enumerate (test_str.split()) if word = = wrd]
if indices:
res = indices[ 0 ]
else :
res = - 1
print ( "The location of word is : " + str (res))
|
Output
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
test_str = 'geeksforgeeks is best for geeks'
wrd = 'best'
words = test_str.split()
for i, word in enumerate (words):
if word = = wrd:
res = i + 1
break
else :
res = - 1
print ( "The location of word is : " + str (res))
|
Output
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.
Please Login to comment...