Run
# Input string x = "Hello World!" # Specific character ch = "r" # Find the index of the character ch_index = x.find(ch) if ch_index != -1: # Get the substring before the character substring = x[:ch_index] else: # Character not found, handle it accordingly substring = "" # Print the result print(substring)
Output