Run
# Input string x = "Hello Wo[Apple123]rld!" # Bracket characters opening_bracket = "[" closing_bracket = "]" # Find indices of brackets index_1 = x.find(opening_bracket) if index_1 != -1: index_2 = x.find(closing_bracket, index_1) if index_1 != -1 and index_2 != -1: # Get the substring between brackets substring = x[index_1 + 1 :index_2] else: print("Error: One or both of the brackets not present") # Print the result print(substring)
Output