KnowledgeBoat Logo
|

Computer Science

Find the output of the following:

word = 'work hard'
result = word.find('work')
print("Substring, 'work', found at index:", result)
result = word.find('har') 
print("Substring, 'har ' ,found at index:", result)
if (word.find('pawan') != -1):
    print("Contains given substring ")
else: 
    print("Doesn't contain given substring")

Python

Python String Manipulation

4 Likes

Answer

Substring, 'work', found at index: 0
Substring, 'har ' ,found at index: 5
Doesn't contain given substring

Working

The code first searches for the substring 'work' in 'work hard' using find(), which is located at index 0, so it prints "Substring, 'work', found at index: 0". Next, it searches for 'har', which starts at index 5, resulting in "Substring, 'har ', found at index: 5". Finally, it checks if 'pawan' is in the string; since it is not, find() returns -1, and the code prints "Doesn't contain given substring".

Answered By

1 Like


Related Questions