Open In App

Converting a 10 digit phone number to US format using Regex in Python

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Text preprocessing is one of the most important tasks in Natural Language Processing. You may want to extract number from a string. Writing a manual script for such processing task requires a lot of effort and at most times it is prone to errors. Keeping in view the importance of these preprocessing tasks, the concept of Regular Expression have been developed in different programming languages in order to ease these text processing tasks.

To implement Regular Expression, the python re package can be used and to be used it can be easily imported like any other inbuilt python module.

Steps for converting a 10-digit phone number to its corresponding US number format:

  • Import the python re package.
  • Write a function that takes the phone number to be formatted as argument and processes it. 
  • Now simply call the function and pass the value.
     

Example:

Python3




import re 
  
def convert_phone_number(phone):
  
  # actual pattern which only change this line
  num = re.sub(r'(?<!\S)(\d{3})-', r'(\1) ', phone) 
  return num
  
# Driver code 
print(convert_phone_number("Call geek 321-963-0612"))


Output:

Call geek (321) 963-0612

Previous Article
Next Article

Similar Reads

Do You Know That Google’s Latest Phone is Not a Smart Phone?
Believe it or not, you are probably addicted to your smartphone!!! In fact, there’s a high chance that your smartphone is currently in your hand or at least in a 1-meter radius! You might even be browsing Facebook or chatting with someone on WhatsApp while reading this article! This smartphone addiction has become so much that a "digital detox" is
4 min read
Parsing and converting HTML documents to XML format using Python
In this article, we are going to see how to parse and convert HTML documents to XML format using Python. It can be done in these ways: Using Ixml module.Using Beautifulsoup module.Method 1: Using the Python lxml library In this approach, we will use Python's lxml library to parse the HTML document and write it to an encoded string representation of
3 min read
The most occurring number in a string using Regex in python
Given a string str, the task is to extract all the numbers from a string and find out the most occurring element of them using Regex Python. It is guaranteed that no two element have the same frequency Examples: Input :geek55of55geeks4abc3dr2 Output :55Input :abcd1def2high2bnasvd3vjhd44Output :2Approach:Extract all the numbers from a string str usi
2 min read
Get Phone Number Information using Python
In this article, you will learn how you can get the information about phone number like the country name to which the phone number belongs to, or the network service provider name of that phone number just by writing a simple Python program. Python provides a module name phonenumbers for this task. This article is Python's port of Google's libphone
2 min read
How to generate a random phone number using Python?
In this article, we will learn how to generate a random phone number using Python. In general, Indian phone numbers are of 10 digits and start with 9, 8, 7, or 6. Approach: We will use the random library to generate random numbers.The number should contain 10 digits.The first digit should start with 9 or 8 or 7 or 6, we will use randint() method.Th
1 min read
Validate an IP address using Python without using RegEx
Given an IP address as input, the task is to write a Python program to check whether the given IP Address is Valid or not without using RegEx. What is an IP (Internet Protocol) Address? Every computer connected to the Internet is identified by a unique four-part string, known as its Internet Protocol (IP) address. An IP address (version 4) consists
2 min read
Python regex | Check whether the input is Floating point number or not
Prerequisite: Regular expression in Python Given an input, write a Python program to check whether the given Input is Floating point number or not. Examples: Input: 1.20 Output: Floating point number Input: -2.356 Output: Floating point number Input: 0.2 Output: Floating point number Input: -3 Output: Not a Floating point number In this program, we
2 min read
Get Country From Phone Number - Python
This article will teach you how to use Python to determine the country of a phone number using Python. How to Get Country From Phone Number in Python using Phonenumbers Module The phonenumbers module is a python package that can be used to find the country corresponding to a phone number based on its phone code. It can also discover basic informati
2 min read
How To Track Phone Number Location With Python
Python is a very powerful language and rich in libraries. So the location of a phone number can be traced with Python using a module named 'phone numbers '. This is one of the modules that provide numerous features like basic information about Python a phone number, the region of the phone number, the operator of the phone number, and other carrier
3 min read
Find all the patterns of “1(0+)1” in a given string using Python Regex
A string contains patterns of the form 1(0+)1 where (0+) represents any non-empty consecutive sequence of 0’s. Count all such patterns. The patterns are allowed to overlap. Note : It contains digits and lowercase characters only. The string is not necessarily a binary. 100201 is not a valid pattern. Examples: Input : 1101001 Output : 2 Input : 1000
2 min read
Practice Tags :
three90RightbarBannerImg