w3resource logo
Python Exercises

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 starting with 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')

Python Code Editor:

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())

Improve this sample solution and post your code through Disqus