jamie_lynn
asked on
How do you substring in Python?
Hi,
I have a address "Santa Mora, AZ 86444
I want to substring this string into city, state and zip but what is the best way to do this?
Do I use partition, rfind, or or split?
Thanks
Jamie
I have a address "Santa Mora, AZ 86444
I want to substring this string into city, state and zip but what is the best way to do this?
Do I use partition, rfind, or or split?
Thanks
Jamie
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
Thanks everyone. I did parse it, but I just wanted to know how everyone else did it to see if I parsed correctly.
>>> s = 'Santa Mora, AZ 86444'
>>> city, XXzip = s.split(',')
>>> state, zip = XXzip.split()
>>> city
'Santa Mora'
>>> state
'AZ'
>>> zip
'86444'
If there are more complicated cases, regular expressions can be also good for extraction of the parts of the address string. However, do it without regular expression if it is easy to do it in the alternative way.