+def replace_nth(string: str, source: str, target: str, nth: int):
+ """Replaces the nth occurrance of a substring within a string.
+
+ >>> replace_nth('this is a test', ' ', '-', 3)
+ 'this is a-test'
+
+ """
+ where = [m.start() for m in re.finditer(source, string)][nth - 1]
+ before = string[:where]
+ after = string[where:]
+ after = after.replace(source, target, 1)
+ return before + after
+
+