How to add remaining string "34defg" to output python

Viewed 37

I am trying to make python take a reveres pattern of the selected input and add the rest in the end. But how do I add the remaining "34defg"? I tried to create a remaining var and print it besides it but it didn't work.

inp1 = "aBcd1234defg"
index = int(input("Enter Index: "))

start = len(inp1)*-1+index
end = (len(inp1)*-1) -1

print(inp1[start:end:-1])
2 Answers

IIUC, here's one way:

inp1 = "aBcd1234defg"
index = int(input("Enter Index: "))
result = inp1[:index+1][::-1] + inp1[index+1:] # prints '21dcBa34defg' for input = 5
inp1 = "aBcd1234defg"
index = int(input("Enter Index: ")) # index=5
start = inp1[:index+1][::-1]    # start=   '21dcBa'
end = inp1[index+1:]            # end=   '34defg'
print(start + end)
Related