Would like to add, don't do this or shoulda used Python.
Manipulating strings in bash is a mistake and creates spaghetti code. This goes for sed and awk as well.
Python equivalent
str_x = "abcde"
pos = str_x.index("d")
ret = str_x[:pos]
print(ret)
In one line, capturing the output into a bash variable
ret=$(python -c 'str_x = "abcde"; pos = str_x.index("d"); ret = str_x[:pos]; print(ret)')