Saturday, 24 August 2024

Phone Number Handling in Python

 

1. Parse and Format a Phone Number

This program parses a phone number and formats it in the international format.


import phonenumbers

from phonenumbers import PhoneNumberFormat, format_number


phone_number = phonenumbers.parse("+14155552671", "US")


formatted_number = format_number(phone_number, PhoneNumberFormat.INTERNATIONAL)


print(f"Formatted Number: {formatted_number}")

Formatted Number: +1 415-555-2671

2. Validate a Phone Number

This program checks whether a phone number is valid or not.


import phonenumbers


phone_number = phonenumbers.parse("+14155552671", "US")


is_valid = phonenumbers.is_valid_number(phone_number)


print(f"Is the phone number valid? {'Yes' if is_valid else 'No'}")

Is the phone number valid? Yes


3. Get the Location of a Phone Number

This program retrieves the location associated with a phone number.


import phonenumbers

from phonenumbers import geocoder


phone_number = phonenumbers.parse("+14155552671", "US")


location = geocoder.description_for_number(phone_number, "en")


print(f"Location: {location}")


#source Code --> clcoding.com

Location: San Francisco, CA

4. Carrier Detection

This program detects the carrier of a given phone number.


import phonenumbers

from phonenumbers import carrier


phone_number = phonenumbers.parse("+14155552671", "US")


phone_carrier = carrier.name_for_number(phone_number, "en")


print(f"Carrier: {phone_carrier}")


#source Code --> clcoding.com

Carrier: 


5. Time Zone Detection

This program retrieves the time zones associated with a phone number.


import phonenumbers

from phonenumbers import timezone


phone_number = phonenumbers.parse("+14155552671", "US")


time_zones = timezone.time_zones_for_number(phone_number)


print(f"Time Zones: {', '.join(time_zones)}")


#source Code --> clcoding.com

Time Zones: America/Los_Angeles

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (49) AI (34) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (226) Cybersecurity (24) data management (11) Data Science (128) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (3) Pandas (4) PHP (20) Projects (29) Python (929) Python Coding Challenge (351) Python Quiz (21) Python Tips (2) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (3) Software (17) SQL (42) UX Research (1) web application (8) Web development (2) web scraping (2)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses