0

My input needs to be for example name surname, street name 22b, 10000 Zagreb

and it need to output like this:

name and surname: name surname
Street: street name
street number: 22
house number: b
postal code: 100000
place: zagreb

and this is my code

whole_string =input("Person: ")
string_list = whole_string.split(", ")
split_street_and_street_number = string_list[1].split(" ")
postal_code_and_city = string_list[2].split(" ")

print(f"name and surname: {string_list[0]}")
print(f"street: {split_street_and_street_number[0]}")
print(f"street number: {split_street_and_street_number[1]}")
print(f"postal code: {postal_code_and_city[0]}")
print(f"city: {postal_code_and_city[1]}")
4
  • 1
    Are you absolutely sure of the formatting of the input? Parsing adresses often brings nightmares because of all the possible typing errors. So a fault tolerant algorithm ends to be close to AI... But if you are sure of your input, it should be possible. Commented Mar 3, 2020 at 16:24
  • Yeah I need to solve this because I need to land a job, a company sent me this as a assignment
    – NikoRan
    Commented Mar 3, 2020 at 16:45
  • 1
    Just an advice from a now old dinosaur. In an interview, it is important to show that you know a programming language. But showing that can think may also matter. So it is close to how you present a physics problem: here is the real world problem, here are the assumptions I will do in order to solve it, here is my solution. And being able to discuss about the limits of the assumptions will show that you will not blindly write code but also will be able to detect problems in the specs. Commented Mar 3, 2020 at 16:54
  • Thanks a lot for the advice, I wrote this one myself, but I searched for a solution for this one and searched I got an idea but don't know how to put it in code, so I'm asking a little help, I understand a lot of programming but sometimes when I can't find solution, I got another 7 assignments and wrote it by myself this one I giving me trouble, hope you understand but thanks a lot I appreciate that a lot
    – NikoRan
    Commented Mar 3, 2020 at 17:19

2 Answers 2

1

Please check out this.

import re

text = '22b'
street_number =" ".join(re.findall("[0-9]+", text))
house_number =" ".join(re.findall("[a-zA-Z]+", text))

print(street_number)
print(house_number)
0
1

You can find the index of the first letter and split the string using that index:

def find_index_of_first_letter(text):
    for index, value in enumerate(text):
        try:
            int(value)
        except ValueError:
            return index
    print('No letter in the text')

text = '22b'
first_letter = find_index_of_first_letter(text)
number, letters = text[:first_letter], text[first_letter:]

1
  • sure ill try this one ;))
    – NikoRan
    Commented Mar 3, 2020 at 16:45

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.