w3resource

Python: Match if two words from a list of words starting with letter 'P'

Python Regular Expression: Exercise-26 with Solution

Write a Python program to match if two words from a list of words start with the letter 'P'.

Sample Solution:

Python Code:

import re

# Sample strings.
words = ["Python PHP", "Java JavaScript", "c c++"]

for w in words:
        m = re.match("(P\w+)\W(P\w+)", w)
        # Check for success
        if m:
            print(m.groups())
			

Sample Output:

('Python', 'PHP')

Pictorial Presentation:

Python: Regular Expression -  Match if two words from a list of words starting with letter 'P'.

Flowchart:

Flowchart: Regular Expression - Match if two words from a list of words starting with letter 'P'<.

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to convert a date of yyyy-mm-dd format to dd-mm-yyyy format.
Next: Write a Python program to separate and print the numbers of a given string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://198.211.115.131/python-exercises/re/python-re-exercise-26.php