Changing the int and string from a string

21,691

Solution 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

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

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

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

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())
Share:
21,691
praneeth sunkavalli
Author by

praneeth sunkavalli

Updated on July 09, 2022

Comments

  • praneeth sunkavalli
    praneeth sunkavalli almost 2 years

    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  
    
  • Samwise
    Samwise almost 4 years
    Good spot! This is a good example of why keeping your comments lined up with the code they comment is good practice; it's really hard to visually follow the vertical line of the indentation because the comments are all over the place.
  • Lagerbaer
    Lagerbaer almost 4 years
    Very nice answer, and goes beyond just fixing the obvious bug directly.
  • Elletlar
    Elletlar over 3 years
    Hi Ghyxen. Welcome to Stack Overflow. While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value. How to Answer. Also, there are a lot of answers for this question. Can you please explain why I should use this one rather than one of the existing answers. Kind Regards.
  • Sven Eberth
    Sven Eberth almost 3 years
    Welcome to StackOverflow. While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
  • arun n a
    arun n a almost 3 years
    here house_number will be single digit always
  • Mayank Raj Vaid
    Mayank Raj Vaid almost 3 years
    @arunna house_number is not always a single digit beacause I have used split() method in line 3.
  • Admin
    Admin over 2 years
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
  • ellhe-blaster
    ellhe-blaster over 2 years
    Please don't add "thank you" as an answer. Once you have sufficient reputation, you will be able to vote up questions and answers that you found helpful. - From Review