• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Strings / Extract a specific word from a string in Python

Extract a specific word from a string in Python

Author: Aditya Raj
Last Updated: March 31, 2021

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 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 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.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Strings Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us