How to Extract a Word from a String in Python

Extracting words from strings is a common task in many programming scenarios. It can be helpful in text analysis, data extraction, and many more applications. Python offers powerful tools for this task.

This tutorial will show you a simple way to extract words from a string in Python.

Step 1: Using split() function

The simplest way to extract words from a string is by using the split() method. This method splits the string according to the specified delimiters and returns a list of substrings.

The output of the above code would be,

['This', 'is', 'a', 'sample', 'text']

Step 2: Choosing Delimiters

By default, the split() method considers whitespace characters (space, tab, newline, etc.) as delimiters. If you want to choose a specific delimiter, you can do so by passing it as an argument to the split() method.

The output of the above code would be,

['This', 'is', 'a', 'sample', 'text']

Step 3: Using regular expressions (re module)

Sometimes the delimiters in the text can be varied and not known beforehand. You can use regular expressions for splitting the string in such scenarios. For more info on regular expressions, please visit this link.

The output of the above code would be,

['This', 'String', 'Has', 'Periods']

Full code

Conclusion

Extracting words from a string in Python is a simple task using the split() method. You can further use regular expressions to deal with more complex scenarios. Regardless of the complexity of the task, Python provides a powerful and flexible toolset to work with strings.