w3resource logo
Python Exercises

Python: Check for a number at the end of a string

Python Regular Expression: Exercise-17 with Solution

Write a Python program to check for a number at the end of a string.

Sample Solution:-

Python Code:

import re
def end_num(string):
    text = re.compile(r".*[0-9]$")
    if text.match(string):
        return True
    else:
        return False

print(end_num('abcdef'))
print(end_num('abcdef6'))

Sample Output:

False                                                                                                         
True

Python Code Editor:

import re
def end_num(string):
    text = re.compile(r".*[0-9]$")
    if text.match(string):
        return True
    else:
        return False

print(end_num('abcdef'))
print(end_num('abcdef6'))

Improve this sample solution and post your code through Disqus