Finding the Longest Word in a String

  • Share this:

Code introduction


This function takes a string as input, uses regular expressions to find all words, and then returns the longest word.


Technology Stack : Regular expressions (re), max

Code Type : Function

Code Difficulty : Intermediate


                
                    
import math
import re

def find_longest_word(text):
    words = re.findall(r'\b\w+\b', text)
    longest_word = max(words, key=len)
    return longest_word