Python | Extract words from given string

Python | Extract words from given string

To extract words from a given string in Python, you can use the split() method of the string class or use regular expressions for more complex scenarios. Here's how you can do it:

1. Using the split() Method:

The simplest way to extract words from a string is to use the split() method, which splits the string at whitespace characters and returns a list of words.

input_string = "Hello, how are you?" words = input_string.split() print(words) 

Output:

['Hello,', 'how', 'are', 'you?'] 

Note that this approach retains punctuation as part of the words.

2. Using Regular Expressions:

If you want to extract words while excluding punctuation, you can use the re module:

import re input_string = "Hello, how are you?" words = re.findall(r'\b\w+\b', input_string) print(words) 

Output:

['Hello', 'how', 'are', 'you'] 

Here's a breakdown of the regex pattern:

  • \b: asserts position at a word boundary.
  • \w: matches any word character (equivalent to [a-zA-Z0-9_]).
  • +: matches between one and unlimited times.

Using regular expressions gives you more flexibility to define what constitutes a "word" based on your specific requirements.


More Tags

layout-inflater lit-html patindex lithium flexbox unicorn api-doc spring-aop google-cloud-composer multimarkdown

More Programming Guides

Other Guides

More Programming Examples