w3resource logo
Python Exercises

Python: Where a string will start with a specific number

Python Regular Expression: Exercise-15 with Solution

Write a Python program where a string will start with a specific number.

Sample Solution:-

Python Code:

import re
def match_num(string):
    text = re.compile(r"^5")
    if text.match(string):
        return True
    else:
        return False
print(match_num('5-2345861'))
print(match_num('6-2345861'))

Sample Output:

True                                                                                                          
False

Python Code Editor:

import re
def match_num(string):
    text = re.compile(r"^5")
    if text.match(string):
        return True
    else:
        return False
print(match_num('5-2345861'))
print(match_num('6-2345861'))

Improve this sample solution and post your code through Disqus