Extract Words from String

PythonPythonBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In Python, a string is a sequence of characters enclosed within single or double quotes. Sometimes, we need to extract individual words from a string. In this challenge, you are tasked with writing a function that takes a string and returns a list of words.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("`Python`")) -.-> python/BasicConceptsGroup(["`Basic Concepts`"]) python(("`Python`")) -.-> python/DataStructuresGroup(["`Data Structures`"]) python(("`Python`")) -.-> python/FunctionsGroup(["`Functions`"]) python(("`Python`")) -.-> python/ModulesandPackagesGroup(["`Modules and Packages`"]) python(("`Python`")) -.-> python/AdvancedTopicsGroup(["`Advanced Topics`"]) python/BasicConceptsGroup -.-> python/comments("`Comments`") python/DataStructuresGroup -.-> python/lists("`Lists`") python/DataStructuresGroup -.-> python/tuples("`Tuples`") python/FunctionsGroup -.-> python/function_definition("`Function Definition`") python/FunctionsGroup -.-> python/default_arguments("`Default Arguments`") python/ModulesandPackagesGroup -.-> python/importing_modules("`Importing Modules`") python/ModulesandPackagesGroup -.-> python/standard_libraries("`Common Standard Libraries`") python/AdvancedTopicsGroup -.-> python/regular_expressions("`Regular Expressions`") subgraph Lab Skills python/comments -.-> lab-13743{{"`Extract Words from String`"}} python/lists -.-> lab-13743{{"`Extract Words from String`"}} python/tuples -.-> lab-13743{{"`Extract Words from String`"}} python/function_definition -.-> lab-13743{{"`Extract Words from String`"}} python/default_arguments -.-> lab-13743{{"`Extract Words from String`"}} python/importing_modules -.-> lab-13743{{"`Extract Words from String`"}} python/standard_libraries -.-> lab-13743{{"`Extract Words from String`"}} python/regular_expressions -.-> lab-13743{{"`Extract Words from String`"}} end

String to Words

Write a function string_to_words(s: str, pattern: str = '[a-zA-Z-]+') -> List[str] that takes a string s and an optional pattern string as arguments and returns a list of words in the string.

  • The function should use re.findall() with the supplied pattern to find all matching substrings.
  • If the pattern argument is not provided, the function should use the default regexp, which matches alphanumeric and hyphens.
import re

def words(s, pattern = '[a-zA-Z-]+'):
  return re.findall(pattern, s)
words('I love Python!!') ## ['I', 'love', 'Python']
words('python, javaScript & coffee') ## ['python', 'javaScript', 'coffee']
words('build -q --out one-item', r'\b[a-zA-Z-]+\b')
## ['build', 'q', 'out', 'one-item']

Summary

In this challenge, you learned how to extract individual words from a string using regular expressions in Python. You can now use this function to split a string into words and perform further operations on them.

Other Python Tutorials you may like