String to Words (Challenge)

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-13211{{"`String to Words (Challenge)`"}} python/lists -.-> lab-13211{{"`String to Words (Challenge)`"}} python/tuples -.-> lab-13211{{"`String to Words (Challenge)`"}} python/function_definition -.-> lab-13211{{"`String to Words (Challenge)`"}} python/default_arguments -.-> lab-13211{{"`String to Words (Challenge)`"}} python/importing_modules -.-> lab-13211{{"`String to Words (Challenge)`"}} python/standard_libraries -.-> lab-13211{{"`String to Words (Challenge)`"}} python/regular_expressions -.-> lab-13211{{"`String to Words (Challenge)`"}} end

String to Words Challenge

Problem

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.

Example

string_to_words('I love Python!!') ## ['I', 'love', 'Python']
string_to_words('python, javaScript & coffee') ## ['python', 'javaScript', 'coffee']
string_to_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