In this assignment, you will practice various methods for manipulating and formatting text in Python using the Thonny IDE. You will replicate examples from class and solve a short coding challenge.
- Open Thonny.
- Create a new file and save it exactly as
OperationsWithStrings.py. - For each string operation covered in the lecture slides, you must:
- Write a
print()statement indicating the operation name (e.g.,print("--- f-strings ---")). - Copy the Python code examples from the slide into your file below that print statement.
- Important: Do NOT copy any Java code examples found in the slides. Only the Python code.
- Run your code frequently to ensure each example works correctly.
At the bottom of your OperationsWithStrings.py file, add a comment # --- CODING EXERCISE --- and solve the following problem:
The Scenario:
You have received a raw data string containing a student's ID and name, but it is surrounded by unnecessary symbols.
raw_text = "###ID:2024-Smith,John###"
Your Task: Write the code to clean this data and print a friendly welcome message.
- Clean it: Remove the
#symbols from both ends usingstrip(). - Extract ID: The ID is the number between the colon
:and the hyphen-(e.g.,2024). Use.find()and slicing to extract it. - Extract Name: The first name is everything after the comma
,(e.g.,John). Use.find()or.rfind()and slicing to extract it. - Final Output: Use an f-string to print the result in this exact format:
"Welcome John! Your student ID is 2024."
- File is named
OperationsWithStrings.py. - All Python examples from the slides are included.
- No Java code is included.
- The Coding Exercise is completed and prints the correct welcome message.
- Upload your file to this GitHub repository.