I am new to Python. Is there a StringTokenizer in Python? Can I do character by character scanning and copying.
I have the following input string
data = '123:Palo Alto, CA -> 456:Seattle, WA 789'
I need to extract the two (city, state) fields from this string. Here is the code I wrote
name_list = []
while i < len(data)):
if line[i] == ':':
name = ''
j = 0
i = i + 1
while line[i] != '-' and line[i].isnumeric() == False:
name[j] = line[i] # This line gives error
i = i + 1
j = j + 1
name_list.append(name)
i = i + 1
What should I do?
My take, assuming the string is always formatted as per your example:
As a note on your error, the empty string will have a length of 0, so the index 0 doesn’t exist:
You can, however, concatenate strings in Python with the
+operator, like so: