def Between(first, second, position, string, direction='forward'):
result = ""
pairs = 1
if direction == 'forward':
for i in string[position+1:]:
if i == first: pairs += 1
elif i == second: pairs -= 1
if pairs==0: break
result = result+i
else:
for i in (string[:position])[::-1]:
if i == second: pairs += 1
elif i == first: pairs -= 1
if pairs==0: break
result = i+result
return result
string = "2(48) = 12(-x)"
print(Between("(", ")", 10, string))
print(Between("(", ")", 4, string, direction="back"))
string = "abc(efg(hij)kl)mno"
position = string.index("(")
print(Between("(", ")", position, string))
string = "abc(efg(hij)kl)mno"
position = len(string) - string[::-1].index(")") - 1
print(Between("(", ")", position, string, direction='back'))