134k views
5 votes
The format_address function separates out parts of the address string into new strings: house_number and street_name, and returns: "house number X on street named Y". The format of the input string is: numeric house number, followed by the street name which may contain numbers, but never by themselves, and could be several words long. For example, "123 Main Street", "1001 1st Ave", or "55 North Center Drive". Fill in the gaps to complete this function.

User Chewy
by
4.1k points

1 Answer

3 votes

Answer:

  1. def format_address(addressStr):
  2. addressComp = addressStr.split(" ")
  3. house_number = addressComp[0]
  4. separator = " "
  5. street_name = separator.join(addressComp[1:])
  6. output = "house number " + house_number + " on street named " + street_name
  7. return output
  8. 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

User Nwpie
by
4.7k points