Q:

Extract the mobile number from the given string in Python

belongs to collection: Python String Programs

0

Python has an inbuilt re module which allows us to solve the various problems based on pattern matching and string manipulation. The re module or regex is an abbreviated form of the regular expression. This module of Python in our daily life is very useful, by using re module you can find the contact number, email-id, special pattern, etc from a file or given sentences. The re module provides a lot of metacharacters and to solve the problem we will use the \d which is used to match any decimal digits(0-9) in the given string or sentence.

Let's take an example to understand the problem better,

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

Algorithm to solve the above problem

  1. Initially, we will import the re module in the program.
  2. Assign the given string or sentence in a variable.
  3. As we all know the contact number or mobile will be of 12 digits that why we will form a format that will use to extract the mobile number.
  4. Now, print the extracted number.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 :  018002089898

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Replace a special string from a given paragraph wi... >>
<< Python | Find the frequency of each character in a...