How will you find, in a string, the first word that rhymes with 'cake'?

Python has a built-in package called re , which can be used to work with Regular Expressions. Import the re module: import re. RegEx in Python.

For our purpose, we will use the function search(), and then use group() to get the

import re
rhyme = re.search(".ake","I would like to make a cake")
print(rhyme.group()) 

Out : 'make'

And as we know, the function search() stops at the first match. Hence, we have our first rhyme to 'cake'.

Comments

Popular posts from this blog

Python Tokens

Pie Chart , Donut Plot, Area plot