- You can't put the variable "state" straight into the replacement string. You should use python string formatting to make reference to the variable.
- Keep regex simple, assume the data are simple. If ZIP is always appear the the end of the string, then just match from the end, use $.
Let me try :
instr = "123 street st, anytown 12345"
# Always strip the trailing spaces to avoid surprises
instr = instr.rstrip()
state = 'CA'
# Assume The ZIP has no trailing space and in last position.
search_pattern = r"(\d{5})$"
#
# Format the replacement, since I search from the end, so group 1 should be fined
replace_str = r"{mystate} \g<1>'.format(mystate = state)
outstr = re.sub(search_pattern, replace_str, instr)
@Forge example is lean and clean. However, you need to be careful about the data quality when using str.rsplit(). For example
# If town and zip code stick together
instr = "123 street st, anytown12345"
# or trailing spaces
instr = "123 street st, anytown 12345 "
The universal fix is use a strip and regex as shown in my code. Always think ahead of input data quality, some code will failed after going through unit test.