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

AI (32) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (146) C (77) C# (12) C++ (82) Course (67) Coursera (198) Cybersecurity (24) data management (11) Data Science (106) Data Strucures (8) Deep Learning (13) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (21) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (46) Meta (18) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (3) PHP (20) Projects (29) Python (888) Python Coding Challenge (285) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

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