Question: Goals In this lab assignment, students will demonstrate the ability to: Create and use strings Use slicing to select ranges of characters in strings Use

Goals

In this lab assignment, students will demonstrate the ability to:

Create and use strings

Use slicing to select ranges of characters in strings

Use string methods and operators

Instructions

In this lab, you will demonstrate your mastery of the string data type.

Follow the instructions in each problem and submit the specified files.

Problem 1 will consist of programs that you create from scratch that meets the problem specification. In Problem 2, you'll be provided starter code that you must complete.

Problems

Problem 1

This program will test your ability to examine and manipulate strings.

Create a file named Lab07P1.py. Write a program that does the following:

Use a loop to ask the user to enter a series of phone numbers in a specific format: ###-###-####. For example, 800-555-1212.

You will need to validate that the phone number is in the correct format. Check for these validation conditions IN THIS ORDER:

Check whether the user entry has exactly two dashes. If it doesn't, display an appropriate error message and continue in the loop. Do not check on other error conditions.

Check whether the first part of the phone number is a three-digit number. If it is not, display an appropriate error message and continue in the loop without checking other error conditions.

Check whether the second part of the phone number is a three-digit number. If it is not, display an appropriate error message and continue in the loop without checking other error conditions.

Check whether the last part of the phone number is a four-digit number. If it is not, display an appropriate error message and continue in the loop without checking other error conditions.

Check whether the number in the first part of the phone number is between 200 and 999 inclusive. If it is not, display an appropriate error message and continue in the loop without checking other error conditions.

If the phone number entered was valid, then change the dashes to periods and display the newly formatted phone number.

For example, if the phone number was: 800-555-1212

the program should display: 800.555.1212

The program will continue asking the user for phone numbers until they enter a q or Q to exit the program.

Here's pseudocode to help you solve this problem:

Ask the user for a phone number

While the user has not entered q to quit

If the phone number does not have 2 dashes

Print "Phone number should have 2 dashes"

Else

Use the split method to split the phone number into parts

If the first part is not all digits or the length of the first part is not 3 characters

Print "First part of phone number must be 3-digit number"

Else if the second part is not all digits or the length of the second part is not 3 characters

Print "First part of phone number must be 3-digit number"

Else if the third part is not all digits or the length of the third part is not 3 characters

Print "First part of phone number must be 3-digit number"

Else if the first part (converted to int) is less than 200

Print "First 3 digits must be between 200 and 999."

Else

Replace the dashes in the phone number with periods

Print the converted phone number with the appropriate message

Ask the user for a phone number

You may want to consider copying the above pseudocode into your Python file and use it as comments that can also guide you as you write this program.

Sample output:

Enter phone number or q to quit: 444-333.5555

Phone number should have 2 dashes.

Enter phone number or q to quit: 44-333-5555

First part of phone number must be a 3-digit number.

Enter phone number or q to quit: 444-3s3-5555

Second part of phone number must be a 3-digit number.

Enter phone number or q to quit: 444-333-888

Last part of phone number must be a 4-digit number.

Enter phone number or q to quit: 444-333-5555

Phone number with dashes replaced: 444.333.5555

Enter phone number or q to quit: 122-333-5555

First 3 digits must be between 200 and 999.

Enter phone number or q to quit: 7777-333-2222

First part of phone number must be a 3-digit number.

Enter phone number or q to quit: 800-555-1212

Phone number with dashes replaced: 800.555.1212

Enter phone number or q to quit: q

Submit the program file Lab07P1.py to Blackboard for credit.

(NO SCREENSHOTS should be submitted for this lab or future labs unless specifically requested.)

Problem 2

This program also tests your ability to analyze and manipulate strings.

You have been provided a starter file called Lab07P2-starter.py. Download that file from Blackboard and rename it Lab07P2.py.

The starter code will open a file named strings.txt if it is in the same directory as your Python program. You can create your own stings.txt file or download the one provided on Blackboard. Note that opening and reading files is something you will learn about in Lesson 8. The starter code will read the file line by line, and pass each line to a function called manipulate_text(). You will be filling in the code for this function.

The manipulate_text() function accepts one string as input. The function should do the following with the string parameter:

Strip the leading and trailing whitespace, and output the string.

Determine if the string is all uppercase, all lowercase, or all digits. Print a message that indicates if any of those characteristics is true or don't print anything if none of the characteristics are true.

Print how many of these characters are in the string: X, z, 7

Concatenate the string "<<< Test" to the end of the string and print the new string.

Print how many characters are in the concatenated string.

For example, if this is the contents of strings.txt:

STYLE SHEETS EXAMPLE 777

forest zipzoom 7 times

778992339

This is the last sentence.

This would be the output of your program:

STYLE SHEETS EXAMPLE 777

String is all uppercase.

The line has 1 X's

The line has 0 z's

The line has 3 7's

STYLE SHEETS EXAMPLE 777<<< Test

The string has 32 characters.

forest zipzoom 7 times

String is all lowercase.

The line has 0 X's

The line has 2 z's

The line has 1 7's

forest zipzoom 7 times<<< Test

The string has 30 characters.

778992339

String is all digits.

The line has 0 X's

The line has 0 z's

The line has 2 7's

778992339<<< Test

The string has 17 characters.

This is the last sentence.

The line has 0 X's

The line has 0 z's

The line has 0 7's

This is the last sentence.<<< Test

The string has 34 characters.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!