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")
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".
Related Questions
Write the output of the following:
string = "Hello Madam, I love Tutorials" substring = "Madam" if string.find(substring) != -1: print("Python found the substring!") else: print("Python did NOT find the substring!")Write the output of the following:
s = "Strings in Python" print(s.capitalize()) print(s.title()) s6 = s.replace("in", "data type") print(s6)Consider the following string mySubject:
mySubject = "Computer Science"What will be the output of the following string operations?
(i) print (mySubject [0:len(mySubject)])
(ii) print (mySubject[-7:-1])
(iii) print (mySubject[::2])
(iv) print (mySubject [len (mySubject) -1])
(v) print (2*mySubject)
(vi) print (mySubject[::-2])
(vii) print (mySubject[:3] + mySubject[3:])
(viii) print (mySubject.swapcase() )
(ix) print (mySubject.startswith('Comp') )
(x) print (mySubject.isalpha() )
Write the Python statement and the output for the following:
(a) Find the third occurrence of 'e' in 'sequence'.
(b) Change the case of each letter in string 'FuNcTioN'.
(c) Whether 'Z' exists in string 'School' or not.