How to Extract Words from a String Longer Than a Given Length
How to Extract Words from a String Longer Than a Given Length
How to Extract Words from a String Longer Than a Given Length
I need to extract all words from a string that are longer than a specified length. How can I do this in Python?
solveurit24@gmail.com Changed status to publish February 16, 2025
You can split the string into words and use a list comprehension to filter based on word length.
Code Example:
def extract_long_words(string, min_length):
words = string.split()
return [word for word in words if len(word) > min_length]
sentence = "Hello world programming is fun"
print(extract_long_words(sentence, 4)) # Output: ['programming']This function returns a list of words from the input string that are longer than the specified minimum length.
solveurit24@gmail.com Changed status to publish February 16, 2025