A5 - Lists and Strings
Reference for this assignment
Python Manual - Pages 8-10
Learning Goals
- Make use of string/list indexing to extract individual characters or items (example -
my_list[3]) - Extract a range of characters/items using colon notation (example -
email_address[0:10]) - Use string and list methods (examples -
my_list.append(7),my_string.lower()) - Use the
lenfunction
Suggested Task
Part A
Begin with a Python program containing the following list of strings:
strings_in = ["ab(def)ghi", "Hello(there)", "12+13(=)25"]
strings_out = []
for string in strings_in:
pass # remove this line and replace with your code
With each string in the list, the program should:
- Output the length of the string
- Output the index of the
(and the index of the)character - Extract the text inside the parentheses, and add that text as a new item in the
strings_outlist.
At the end, you should print the strings_out list.
The output should look something like this (the formatting doesn't need to match exactly):
string length is 10 | index of '(' is 2 | index of ')' is 6
string length is 12 | index of '(' is 5 | index of ')' is 11
string length is 10 | index of '(' is 5 | index of ')' is 7
['def', 'there', '=']
Part B
Write your own program that will somehow usefully manipulate a list of strings, and store the results in a new list of strings.
For example, here is a program what will take a list of email addresses, extract the part that comes before the @ sign, and make all letters lowercase:
emails = ["dcstewart@scdsb.on.ca", "ME@example.com", "PineAPPle@tropical-fruit.com"]
result = []
for email in emails:
at_index = email.find("@")
first_part = email[0:at_index]
first_part_lowercase = first_part.lower()
result.append(first_part_lowercase)
print(result)
# Output is ['dcstewart', 'me', 'pineapple']