This is to use Python I create a list withe the building names 'Richard Weeks Hall’, ’Busch Student Center’, ‘Environmental & Natural Resource Sciences Building’ How would I code for this? • If the name contains ‘Hall’, remove the word ‘Hall’ from the name. • If the name contains ‘Building’, replace it with ‘Bldg’.
This is to use Python
I create a list withe the building names
'Richard Weeks Hall’, ’Busch Student Center’, ‘Environmental & Natural Resource Sciences Building’
How would I code for this?
• If the name contains ‘Hall’, remove the word ‘Hall’ from the name.
• If the name contains ‘Building’, replace it with ‘Bldg’.
Following is the python program:
Approach:
- create a list to store the names of the building.
- using for loop print the names of the list before modification.
- use the replace() function to replace the substring Hall with null and replace the substring Building with Bldg.
Code:
# creating a list having building names.
strings = ["Richard Weeks Hall", "Busch Student Center", "Environmental & Natural Resource Sciences Building"]
# Display all string elements in list.
for st in strings:
print(st)
# Replace substring in list of strings
res = [sub.replace('Hall', '').replace('Building', 'Bldg') for sub in strings]
# print result
print("\nThe list after substring replacement : ")
for st in res:
print(st)
Step by step
Solved in 3 steps with 2 images