Answer:
- def format_address(addressStr):
- addressComp = addressStr.split(" ")
- house_number = addressComp[0]
- separator = " "
- street_name = separator.join(addressComp[1:])
-
- output = "house number " + house_number + " on street named " + street_name
- return output
-
- print(format_address("123 Main Street"))
Step-by-step explanation:
Firstly create a function format_address that take one input address string (Line 1). Next, use split method to divide the address string into a list of strings (Line 2). Use index-0 to get the first string which is a street number and assign it to house_number variable (Line 3). Next use join method to combine the subsequent list items from index 1 into a single string which is a street name and assign it to a variable (Line 4-5). At last, generate the output string and return it as result of the function (Line 7-8).
We test the function by using input string "123 Main Street" and we shall get
house number 123 on street named Main Street