We sometimes come through the situations where we require to get all the works present in the string, this can be a tedious task done using naive method. Hence having shorthands to perform this task is always useful. Additionally this article also includes the cases in which punctuation marks have to be ignored.
Method #1 : Using split()
Using split function, we can split the string into a list of words and is most generic and recommended method if one wished to accomplish this particular task. But drawback is that it fails in the cases in string contains punctuation marks.
# Python3 code to demonstrate # to extract words from string # using split() # initializing string test_string = "Geeksforgeeks is best Computer Science Portal" # printing original string print ("The original string is : " + test_string) # using split() # to extract words from string res = test_string.split() # printing result print ("The list of words is : " + str(res)) |
The original string is : Geeksforgeeks is best Computer Science Portal
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
Method #2 : Using regex( findall() )
In the cases which contain all the special characters and punctuation marks, as discussed above, the conventional method of finding words in string using split can fail and hence requires regular expressions to perform this task. findall function returns the list after filtering the string and extracting words ignoring punctuation marks.
# Python3 code to demonstrate # to extract words from string # using regex( findall() ) import re # initializing string test_string = "Geeksforgeeks, is best @# Computer Science Portal.!!!" # printing original string print ("The original string is : " + test_string) # using regex( findall() ) # to extract words from string res = re.findall(r'\w+', test_string) # printing result print ("The list of words is : " + str(res)) |
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
Method #3 : Using regex() + string.punctuation
This method also used regular expressions, but string function of getting all the punctuations is used to ignore all the punctuation marks and get the filtered result string.
# Python3 code to demonstrate # to extract words from string # using regex() + string.punctuation import re import string # initializing string test_string = "Geeksforgeeks, is best @# Computer Science Portal.!!!" # printing original string print ("The original string is : " + test_string) # using regex() + string.punctuation # to extract words from string res = re.sub('['+string.punctuation+']', '', test_string).split() # printing result print ("The list of words is : " + str(res)) |
The original string is : Geeksforgeeks, is best @# Computer Science Portal.!!!
The list of words is : [‘Geeksforgeeks’, ‘is’, ‘best’, ‘Computer’, ‘Science’, ‘Portal’]
Recommended Posts:
- Python | Extract odd length words in String
- Python - Extract words starting with K in String List
- Python | Extract Nth words in Strings List
- Python | Extract only characters from given string
- Python | Extract digits from given string
- Python Regex to extract maximum numeric value from a string
- Python | Extract numbers from string
- Python | Extract length of longest string in list
- Python | Extract Score list of String
- Python | Extract Numbers in Brackets in String
- Python | Extract characters except of K string
- Python - Extract String till Numeric
- Python - Extract Percentages from String
- Python - Extract range characters from String
- Reverse words in a given String in Python
- Python Slicing | Extract ‘k’ bits from a given position
- Count words in a given string
- Extract date from a specified column of a given Pandas DataFrame using Regex
- Python program to print even length words in a string
- Iterate over words of a String in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

