📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    Complete the Python function to find the starting index

    of the first occurrence of sub in main_string. Return -1 if not found. def find_substring_index(main_string, sub):     _________ # Line to complete
    A return main_string.find(sub) Correct Answer Incorrect Answer
    B return main_string.index(sub) Correct Answer Incorrect Answer
    C return main_string.search(sub) Correct Answer Incorrect Answer
    D return main_string.locate(sub) Correct Answer Incorrect Answer
    E return main_string.contains(sub) Correct Answer Incorrect Answer

    Solution

    Correct Answer: A (str.find() returns -1 if not found, str.index() raises a ValueError.)

    Practice Next
    ask-question