Computer Science
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!")
Python
Python String Manipulation
4 Likes
Answer
Python found the substring!
Working
The code checks if the substring "Madam" exists in the string "Hello Madam, I love Tutorials" using the find() method. Since "Madam" is present in the string, find() returns the starting index of "Madam", which is not -1. Therefore, the condition string.find(substring) != -1 evaluates to True, and the code prints "Python found the substring!".
Answered By
2 Likes
Related Questions
How many times is the word 'HELLO' printed in the following statement?
s = "python rocks" for ch in s: print ("Hello")Write the output of the following:
>>> x = "hello" >>> print(x[1:-2])Write the output of the following:
s = "Strings in Python" print(s.capitalize()) print(s.title()) s6 = s.replace("in", "data type") print(s6)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")