Knowee
Questions
Features
Study Tools

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)

Question

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)

🧐 Not the exact question you are looking for?Go ask a question

Solution

The output of the code will be:

['an', 'apple', 'a', 'day', 'keeps', 'away']

Here's the step by step explanation:

  1. The string "an apple a day keeps doctor away" is split into a list of words using the split() function. The split() function splits a string into a list where each word is a list item. In this case, the separator is a space character " ".

  2. The list 'lis' now contains the following items: ['an', 'apple', 'a', 'day', 'keeps', 'doctor', 'away'].

  3. The for loop iterates over each word in the list. If the word starts with the letter "d", it is removed from the list.

  4. The word "doctor" starts with "d", so it is removed from the list.

  5. The final list, which is printed, is ['an', 'apple', 'a', 'day', 'keeps', 'away'].

This problem has been solved

Similar Questions

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=" ")

What is the output of the following program?line = "What will have so will" L = line.split('a') for i in L: print(i, end=' ')

Write a Python program to split a string to words

In the first line of input, accept a sequence of space-separated words. In the second line of input, accept a single word. If this word is not present in the sequence, print NO. If this word is present in the sequence, then print YES and in the next line of the output, print the number of times the word appears in the sequence.words = input().split('')test = input()if test not in words:    Print('No')else:    Print('YES')    count = 0    for word in words:        if test == word:            count -= 1                    Print(count)Sample Test CasesDownload All Test Case 1InputExpected OutputActual Outputa good collection of wordstheNOTest Case 2InputExpected OutputActual Outputno sentence can begin with because because because is a conjunctionbecauseYES3

Write a Python program to split a string to wordsstring = "Hello World"print(string.split())string = "Hello World"print(string.split("o"))string = "Hello World"print(string.split("l"))string = "Hello World"print(string.split("W"))

1/3

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.