Extract a specific word from a string in Python

While handling text data, sometimes we have to search for occurrences of specific words in the text and extract specific words. In this tutorial, we will learn about different methods to extract a specific word from a string in python using inbuilt string methods and regular expressions.So, let’s dive into it.

Extract a specific word from a string using string slicing in python

If we know the exact position of the word to be extracted from the string, we can perform a slicing operation on the string to extract the desired word from the string as shown below.

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print("String from which word has to be searched is:")
print(search_string)
print("word to be extracted from string:")
word="writing"
print(word)
#calculate length of the word
lword=len(word)
#suppose we already know the starting index of word writing i.e. 34
extracted_string= search_string[34:34+lword]
print("Extracted word is:")
print(extracted_string)

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
Extracted word is:
writing

Extract a specific word from a string using find() method.

If we want to extract a specific word from the string and we do not know the exact position of the word, we can first find the position of the word using find() method and then we can extract the word using string slicing.

The find() method when invoked on any string, takes the string to be searched as a parameter and gives as output the position of first occurrence of the input string which was to be searched. If the string to be searched is not present, the find() method returns -1.

After finding the position of the word to be extracted with find() method, we can simply extract it using slice operation as follows.

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print("String from which word has to be searched is:")
print(search_string)
print("word to be extracted from string:")
word="writing"
print(word)
#calculate length of the word
lword=len(word)
start_index=search_string.find(word)
print("start index of the word in string is:")
print(start_index)
extracted_string= search_string[start_index:start_index+lword]
print("Extracted word is:")
print(extracted_string)

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing

Using index() method.

If we don’t know the exact position of the word to be extracted,we can also use string index() method to find out the exact position of the word and then we can use slicing to extract the word.

The index() method when invoked on any string, takes the string to be searched as a parameter and gives as output the position of first occurrence of the input string which was to be searched. If the string to be searched is not present, index() throws an exception. For this reason we will have to use python try except to handle the exceptions while using index() method.

We can extract a specific word from a string in python using index() method and string slicing as follows.

search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print("String from which word has to be searched is:")
print(search_string)
print("word to be extracted from string:")
word="writing"
print(word)
#calculate length of the word
lword=len(word)
try:
    
    start_index=search_string.index(word)
    print("start index of the word in string is:")
    print(start_index)
    extracted_string= search_string[start_index:start_index+lword]
    print("Extracted word is:")
    print(extracted_string)
except:
    print("word not found")

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing

Using regular expressions to extract any specific word

We can use regular expressions in python to extract specific words from a string. We can use search() method from re module to find the first occurrence of the word and then we can obtain the word using slicing.

re.search() method  will take the word to be extracted in regular expression form and the string as input and returns a re.MatchObject which contains the starting and ending index of the word.If the given word is not found, re.search() will return None. After getting the indices of the word to be extracted, we can extract it using string slicing as shown below.

import re
search_string= "I am a python programmer and I am writing this code for pythonforbeginners.com"
print("String from which word has to be searched is:")
print(search_string)
print("word to be extracted from string:")
word=r"writing"
print(word)
#calculate length of the word
lword=len(word)
start_index=re.search(word,search_string).start()
print("start index of the word in string is:")
print(start_index)
extracted_string= search_string[start_index:start_index+lword]
print("Extracted word is:")
print(extracted_string)

Output:

String from which word has to be searched is:
I am a python programmer and I am writing this code for pythonforbeginners.com
word to be extracted from string:
writing
start index of the word in string is:
34
Extracted word is:
writing

Conclusion

In this article, we have seen how to find any specific word in a string using different string methods and regular expressions and then print the word using string slicing in python. We can also use python string split operation when we just have to search if the word is present or not, given that words are space separated. Stay tuned for more informative articles.

Leave a Reply

Your email address will not be published. Required fields are marked *