What will be the output of the following code?str1=" An apple a day keeps doctor away"print(str1.find("a"),end=" ")print(str1.find("m"),end=" ")print(str1.find("day"),end=" ")str1=str1[ : :-1]print(str1.find("y"),end=" ")
Question
What will be the output of the following code?str1=" An apple a day keeps doctor away"print(str1.find("a"),end=" ")print(str1.find("m"),end=" ")print(str1.find("day"),end=" ")str1=str1[ : :-1]print(str1.find("y"),end=" ")
Solution
The output of the code will be:
4 -1 12 2
Here's the step by step explanation:
-
The
find()function in Python is used to find the index of a substring in a string. It returns the lowest index where the substring is found. If the substring is not found, it returns -1. -
str1.find("a")will return 4 because the first occurrence of "a" (ignoring case) is at the 4th index. -
str1.find("m")will return -1 because "m" is not found in the string. -
str1.find("day")will return 12 because "day" starts at the 12th index. -
str1[ : :-1]will reverse the string. So, the string becomes "yawa rotcod speek yad a elppa nA". -
str1.find("y")will return 2 because after reversing the string, the first occurrence of "y" is at the 2nd index.
Similar Questions
What will be the output of the following code?str1="HellO{}niNja"str1 = str1.replace(" {} ", " ")str1=str1.lower()pos=str1.find("o")print(str1+str(pos))
2. What will be the output of the following Python code?>>>str="hello">>>str[:2]>>>
What will be the output of the following code? str1="an apple a day keeps doctor away"lis=str1.split(" ")for word in lis: if word.startswith("d") : lis.remove(word)print(lis)
What is the output of the Python code below?s = "help"for letter in s[1:]: last = letter breakprint(last)Question 2Select one:a.hb.ec.!d.pe.l
What is the output of the following code? word = "Python" print(word[::-1])
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.