'Changing the int and string from a string

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.

def format_address(address_string):
  # Declare variables
  house_number=' '
  street_name=" "

  # Separate the address string into parts
  x=address_string.split(" ")
  # Traverse through the address parts
  for y in x:
    if(y.isdigit()):
      house_number=y
    else:
      street_name+=y
      street_name+=' '
    # Determine if the address part is the
    # house number or part of the street name
 # Does anything else need to be done 
  # before returning the result?
  
  # Return the formatted string  
    return "house number {} on street named {}".format(house_number,street_name)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

But it is showing output as :

house number 123 on street named  
house number 1001 on street named  
house number 55 on street named  


Solution 1:[1]

When you use str.split(), you have the option to choose how many segments you want to split the string to (as long as it's not over the maximum). In your case, the string should only be split once, to separate the house number from the street name. Also, you can a formatted string:

def format_address(address_string):
    num, st = address_string.split(' ',1)
    return f"house number {num} on street named {st}"

print(format_address("123 Main Street"))

Output:

house number 123 on street named Main Street

Solution 2:[2]

it looks to me like your return statement happens inside the for loop, which means you are exiting the loop after just the first item! But by that time you haven't identified the street name yet.

Just move the return ... back a tab and it should work fine.

Solution 3:[3]

def format_address(address_string):
  # Declare variables
  house_number = 0
  street_name = []
  # Separate the address string into parts
  address = address_string.split()
  # Traverse through the address parts
  for item in address:
    if item.isnumeric():
      house_number = item
    else:
      street_name.append(item)
    # Determine if the address part is the
    # house number or part of the street name

  # Does anything else need to be done 
  # before returning the result?
  
  # Return the formatted string  
  return "house number {} on street named {}".format(house_number, " ".join(street_name))

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 4:[4]

def format_address(address_string):
  # Declare variables
  number = ''
  street = ''
  # Separate the address string into parts
  address_string = address_string.split()
  # Traverse through the address parts
  for add in address_string:
      # Determine if the address part is the
      # house number or part of the street name
      if add.isdigit():
          number += add 
      # Does anything else need to be done 
      # before returning the result?
      else :
          street += " " + add
      # Return the formatted string  
      return "house number {} on street named {}".format(number, street)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 5:[5]

def format_address(address_string):
# Declare variables
house_number=''
street_name=''
# Separate the address string into parts
address = address_string.split()
# Traverse through the address parts
for add in address:
    # Determine if the address part is the
    # house number or part of the street name
    if add.isnumeric():
        house_number = int(add)
    else:
        street_name += add + " "
        # Does anything else need to be done 
        # before returning the result?
    # Return the formatted string
return "house number {} on street named {}".format(house_number,street_name.strip())

Solution 6:[6]

Actually managed to get it...right? without external help, but it didn´t pass because the function prints a space after every element, so for example it showed "house number 123 on street named Main Street ", so i tried to fix it:

def format_address(address_string):
    # Declare variables
    house_number=""
    street_name=""
    lista=address_string.split()
    # Separate the address string into parts
    # Traverse through the address parts
    for x in lista:
        if x.upper()==x.lower(): #didn´t know about .isdigit() at the time 
        house_number=x
    elif x==lista[-1]:  #takes the space out if it is the last element of the list
        street_name+=x
    else:
        street_name+=x+" "
        # Determine if the address part is the
        # house number or part of the street name
        # Does anything else need to be done 
        # before returning the result?
        # Return the formatted string  
    return "house number {} on street named {}".format(house_number, street_name)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

Solution 7:[7]

It's because your return is not in the good indentation. It's called in the first loop so your boucle for did just one iteration.

def format_address(address_string):
  # Declare variables
  house_number=' '
  street_name=" "

  # Separate the address string into parts
  x=address_string.split(" ")
  # Traverse through the address parts
  for y in x:
    if(y.isdigit()):
      house_number=y
    else:
      street_name+=y
      street_name+=' '
    # Determine if the address part is the
    # house number or part of the street name
 # Does anything else need to be done 
  # before returning the result?

  # Return the formatted string  
  return "house number {} on street named {}".format(house_number,street_name)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"```

Solution 8:[8]

Or you can just do this!

def format_address(address_string):
  # Declare variables
  space = address_string.find(" ")
  house_number = address_string[:space]
  street_name = address_string[space:]
  return "house number {} on street named{}".format(house_number, street_name)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 9:[9]

def format_address(address_string):
  house_number =''
  street_name =''

  # Declare variables

  # Separate the address string into parts
  spi = address_string.split()
  # Traverse through the address parts
  for ele in spi:
    if ele.isdigit():
      house_number = ele
    else:
      street_name += ele
      street_name += ' '
`

`# Return the formatted string`
  `return "house number {} on street named {}".format(house_number, street_name)`


print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"`

Solution 10:[10]

This works for me

def format_address(address_string):
    # Declare variables
    hn =""
    ha =""
  # Separate the address string into parts
    address_string = address_string.split()
  # Traverse through the address parts
    for add in address_string:
        if add.isnumeric():
            hn += add
        else:
            ha += add + " "
    # Determine if the address part is the
    # house number or part of the street name
  # Does anything else need to be done
  # before returning the result?

  # Return the formatted string
    return "house number {} on street named {}".format(hn,ha)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 11:[11]

def format_address(address_string):
# Declare variables
  house_number=[]
  street_name=[]
  addresses=[]
# Separate the address string into parts
  addresses = address_string.split()
# Traverse through the address parts
  for address in addresses:
# Determine if the address part is the house number or part of the street name
    
    if address.isnumeric() == True:
       house_number.append(address)
    else:
       street_name.append(address)
  
  house=" ".join(house_number)
  street=" ".join(street_name)
# Return the formatted string  
  return "house number {} on street named {}".format(house,street)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 12:[12]

def format_address(address_string):
  words = address_string.split()
  return "house number {} on street named {}".format(words[0],' '.join(words[1:]))

Solution 13:[13]

def format_address(address_string):
  # Declare variables
  addres_list = []
  name = " "
  # Separate the address string into parts
  addres_list = address_string.split(" ")
  # Traverse through the address parts
  number = addres_list[0]
  name = name.join(addres_list[1:])
  # Return the formatted string  
  return "house number {} on street named {}".format(number,name)

Solution 14:[14]

def format_address(address_string):

  address = address_string.split(" ",1)
  house=address[0]
  street=address[1]
  return "house number {} on street named {}".format(house,street)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 15:[15]

I solve it without using if statement for numeric , i splits the list to 2 parts and add each part to the appropriate variable.

def format_address(address_string):
      house_number = ''
      street_name = ''
      parts = address_string.split(' ',1)
      house_number += parts[0]
      street_name += parts[1]
    
    
      return "house number {} on street named {}".format(house_number,street_name)

Solution 16:[16]

def format_address(address_string):
     # Declare variables
     house_number =' '
     street_name =" "

     # Separate the address string into parts
     x= address_string.split(" ")
      # Traverse through the address parts
      for y in x:
        if(y.isdigit()):
          house_number=y
        else:
          street_name+=y
          street_name+=' '
    # Determine if the address part is the
    # house number or part of the street name
    # Does anything else need to be done
    # before returning the result?

    # Return the formatted string
    return "house number {} on street named {}".format(house_number, street_name)

    print(format_address("123 Main Street"))
    # Should print: "house number 123 on street named Main Street"

    print(format_address("1001 1st Ave"))
    # Should print: "house number 1001 on stre
    print(format_address("55 North Center Drive"))
    # Should print "house number 55 on street named North Center Drive"

Solution 17:[17]

def format_address(address_string):
  street_name = ""
  houseandstreet = address_string.split()
  for i in houseandstreet:
    if i.isdigit():
      house_number = i
    else:
      street_name = street_name + i +" "   
  return "house number {} on street named {}".format(house_number,street_name)

Solution 18:[18]

I'll chip in, too. Sometimes the number isn't at the beginning, so the shortest answer here doesn't work in that case.

Here's my version:

def format_address(address_string):
  # Declare variables
    house_nr = ""
    street = ""
  # Separate the address string into parts
    addy_parts = address_string.split()
  # Traverse through the address parts
    for p in addy_parts:
    # Determine if the address part is the
    # house number or part of the street name
        if p.isnumeric() and house_nr == "":
            house_nr = p
        else:
            street += p + " "
            
  # Does anything else need to be done 
  # before returning the result?
  
  # Return the formatted string  
    return "house number {} on street named {street}".format(house_nr, street = street.rstrip())

print(format_address("Main Street 123"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave Apt 20"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

It will also take into account more than 1 numeric word in the address, e.g. when an appartment number is given, or whatever silly entry a person gives.

I know this isn't part of the original question in the Google Course this Q is from, but it helps understanding how it works.

If you just want the short answer, use @Ann Zen's answer, it's nice.

Solution 19:[19]

def format_address(address_string):
  # Declare variables
  z = address_string.split(" ")
  house_number = z[0]
  street_name = z[1:]
  # Return the formatted string  
  return "house number {} on street named {}".format(house_number, " ".join(street_name))

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 20:[20]

x[0] will be the house number, while " ".join(x[1:]) will be the street name. This works because number is always first in the string.

def format_address(address_string):
    x = address_string.split()
    return "house number {} on street named {}".format(x[0], " ".join(x[1:]))

print(format_address("123 Main Street"))
print(format_address("1001 1st Ave"))
print(format_address("55 North Center Drive"))

Solution 21:[21]

def format_address(address_string):

return "house number {} on street named {}".format(address_string.split()[0], " ".join(address_string.split()[1:]))

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"

Solution 22:[22]

This worked for me. I added street_name_raw as a list to capture the whole street address and then use join() to concatenate the words on the list.

def format_address(address_string):
  # Declare variables
  house_number = ""
  street_name = ""
  street_name_raw = []
  # Separate the address string into parts
  address = address_string.split(" ")
  # Traverse through the address parts
  for item in address:
    if item.isnumeric():
      house_number = item
    else:
      street_name_raw.append(item)
      street_name = " ".join(street_name_raw)

    # Determine if the address part is the
    # house number or part of the street name

  # Does anything else need to be done 
  # before returning the result?
  
  # Return the formatted string  
  return "house number {} on street named {}".format(house_number, street_name)

print(format_address("123 Main Street"))
# Should print: "house number 123 on street named Main Street"

print(format_address("1001 1st Ave"))
# Should print: "house number 1001 on street named 1st Ave"

print(format_address("55 North Center Drive"))
# Should print "house number 55 on street named North Center Drive"