Skip to content

Instantly share code, notes, and snippets.

  • Save AnisahTiaraPratiwi/360fc50e073ea4a8fc967ddb511c6dbd to your computer and use it in GitHub Desktop.
Save AnisahTiaraPratiwi/360fc50e073ea4a8fc967ddb511c6dbd to your computer and use it in GitHub Desktop.
Strings_Lists_Dictionaries_1.py
def format_address(address_string):
# Declare variables
house_number = ""
street_name = ""
# Separate the address string into parts
address_string = address_string.split()
# Traverse through the address parts
for number in address_string:
# Determine if the address part is the
# house number or part of the street name
if number.isdigit():
house_number = number
# Does anything else need to be done
# before returning the result?
else:
street_name += number
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"
@fathialibi
Copy link

def format_address(address_string):

Declare variables

Separate the address string into parts

Traverse through the address parts

address=address_string.split(" ",1)
for x in address:
# 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

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

return a
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"

@DEVTOSHPALEI
Copy link

Here is the Output:

house number 123 on street named Main Street
house number 1001 on street named 1st Ave
house number 55 on street named North Center Drive

Thank You

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment