If Str is a Python string as Str ='Hello World!' What will be the output of…

2023

If Str is a Python string as Str ='Hello World!' What will be the output of the following Python Command? print(Str.find('l'))

  1. A.

    2 / 2

  2. B.

    3 / 3

  3. C.

    [2, 3, 7] / [2, 3, 7]

  4. D.

    (2, 3, 7) / (2, 3, 7)

Attempted by 1367 students.

Show answer & explanation

Correct answer: A

Answer: 2

Explanation: str.find(sub) returns the index (zero-based) of the first occurrence of sub in the string, or -1 if not found. For Str = 'Hello World!' the character indexes are:

  • 0: H

  • 1: e

  • 2: l

  • 3: l

  • 4: o

  • 5: (space)

  • 6: W

  • 7: o

  • 8: r

  • 9: l

  • 10: d

  • 11: !

The first 'l' appears at index 2, so print(Str.find('l')) outputs 2. Note: if the substring were not present, find would return -1.

The option that shows 2 / 2 is the correct choice; the option showing 3 / 3 is incorrect due to counting from 1 instead of using Python's zero-based indexing.

Explore the full course: Rssb Senior Computer Instructor