Skip to content Skip to sidebar Skip to footer

11.11 Required - Lab 11b: Phone Number Breakdown With Functions

Phone Number Breakdown

Do you ever wonder what all those digits mean in a phone number? Have you ever needed to extract certain information from a phone number, like the area code or the exchange? In this lab, we will be breaking down phone numbers using functions in Python.

What is a Phone Number?

Phone Number

A phone number is a sequence of digits assigned to a telephone line that connects two or more devices for the purpose of voice or data communication. In the United States, a standard phone number consists of ten digits: three for the area code, three for the exchange, and four for the subscriber number.

Breaking Down a Phone Number

Phone Number Breakdown

To break down a phone number into its component parts, we will be using functions in Python. A function is a block of code that performs a specific task and can be reused throughout your program. We will be creating several functions to extract the area code, exchange, and subscriber number from a phone number.

Extracting the Area Code

Area Code

The area code is the first three digits of a phone number and identifies the geographic region of the telephone line. To extract the area code from a phone number, we will create a function called get_area_code():

def get_area_code(phone_number):return phone_number[:3]

This function takes a phone number as its argument and returns the first three digits using Python's string slicing notation.

Extracting the Exchange

Telephone Exchange

The exchange is the three digits following the area code and identifies the specific central office that serves the telephone line. To extract the exchange from a phone number, we will create a function called get_exchange():

def get_exchange(phone_number):return phone_number[3:6]

This function takes a phone number as its argument and returns the three digits starting at index 3 and ending at index 5.

Extracting the Subscriber Number

Subscriber Number

The subscriber number is the four digits following the exchange and identifies the specific line connected to the central office. To extract the subscriber number from a phone number, we will create a function called get_subscriber_number():

def get_subscriber_number(phone_number):return phone_number[6:]

This function takes a phone number as its argument and returns the four digits starting at index 6 and ending at the end of the string.

Putting It All Together

Phone Number Breakdown

Now that we have our three functions to extract the area code, exchange, and subscriber number from a phone number, we can use them to break down any phone number into its component parts. Here is an example:

phone_number = "5551234567"area_code = get_area_code(phone_number)exchange = get_exchange(phone_number)subscriber_number = get_subscriber_number(phone_number)print("Phone Number Breakdown:")print("Area Code: {}".format(area_code))print("Exchange: {}".format(exchange))print("Subscriber Number: {}".format(subscriber_number))

The output of this code will be:

Phone Number Breakdown:Area Code: 555Exchange: 123Subscriber Number: 4567

Conclusion

In this lab, we learned how to break down a phone number into its component parts using functions in Python. We created three functions to extract the area code, exchange, and subscriber number from a phone number, and then used them to break down a sample phone number. With this knowledge, you can now extract any information you need from a phone number and use it in your Python programs.

Related video of 11.11 Required - Lab 11b: Phone Number Breakdown With Functions