Hackerrank Python Solutions

Validating Phone Numbers Hackerrank Solution In Python

Last updated March 7, 2024 by Jarvis Silva

In this article, I will share the validating phone numbers hackerrank solution in python. Hackerrank is a popular online platform which has many programming challenges and competitions which allows developers to participate into them and improve their programming skill.


A phone number contains 10 digit and starts with 7, 8 or 9 so you need to write a code to validate phone number using regex.

You can find the full question of this problem here: Validating Phone Number Hackerrank Challenge Question.

In this challenge we need to use regex to validate phone number in python to do that we will use the re module which allows us to create regex in python.

Before moving to the solution you should definately try to do this on your own it is very easy but still if you can’t solve it don’t worry you can check the below solution and understand this program.


import re

number = int(input())

for _ in range(number):
    pattern = r'^[789]\d{9}$'
    print ( 'YES' if re.match(pattern,input()) else 'NO')

Above is the python solution for the validating phone numbers Hackerrank challenge, you can submit the above code in hackerrank and it should show you congratulations you solved this challenge.

So this was for this article, I hope you found what you were looking and if you want more hackerrank solutions then visit here: Hackerrank python solutions.

Thank you for reading, Happy coding 😊