Extract the mobile number from the given string in Python

Extract the mobile number from the given string in Python

Here, we will figure out how to separate the portable/telephone number from a given string by utilizing the re module in the Python programming language?

To take care of this issue effectively, we will utilize the re module in the program.

A string will be given by the client and we need to extricate the portable number by utilizing the Python programming language.

Prior to going to utilize the re module, we will get familiar with a tad about re module.

Python has an inbuilt re module which enables us to tackle the different issues dependent on design coordinating and string control.

The re module or regex is an abridged type of the ordinary articulation. This module of Python in our everyday life is valuable, by utilizing re module you can discover the contact number, email-id, exceptional example, and so on from a document or given sentences.

The re module gives a ton of metacharacters and to tackle the issue we will utilize the \d which is utilized to coordinate any decimal digits(0-9) in the given string or sentence.

We should take an example to comprehend the issue better,

    Input: 
    "Hello! I am Kishan Kaushik and my contact number is 918889050068. 
    May i know the call logs of my number"
    
    Output: 
    918889050068
    
    Explanation: 
    The program will have to find a substring that contains 
    12 digit substring and print it.

The calculation to tackle the above issue

At first, we will import the re module in the program.

Allocate the given string or sentence in a variable.

As we as a whole realize the contact number or portable will be of 12 digits that why we will shape a configuration that will use to extricate the versatile number.

Presently, print the separated number.

Program:

# importing the module
import re

# string
string='''If you would like to get in touch with us through other ways, 
the Flipkart customer support number is 018002089898. 
And we're just a call away if you need anything. 
You can also arrange a call-back from within the 
Flipkart app regarding any issue related to your order.'''

# extracting the mobile number
Phonenumber=re.compile(r'\d\d\d\d\d\d\d\d\d\d\d\d')
m=Phonenumber.search(string)

# printing the result 
print('mobile number found from the string : ',m.group())

Output:

mobile number found from the string :  918889050068

Recent Posts

error: Alert: Content is protected!!