Switch str.rpartition to search from left to right. Factor the duplicate code into one helper function.