Reading Time: 1 minutes
Finding words of length from 2 to 4 in Python
Finding words of length from 2 to 4: Using Python, write a short program to find all words in a string which are either 2-letter long, 3-letter long or 4-letter long.
words of length from 2 to 4
import re sourceString = 'on two three four five six seven' print( re.findall(r'\b\w{2,4}\b', sourceString) ) # ['on', 'two', 'four', 'five', 'six']
To know more about Regular Expressions in Python, click here.