CBSE Class 11 Computer Science Question 4 of 19

Strings in Python — Question 15

Back to all questions
15
Question

Question 15

STR = "RGBCOLOR"
colors = list(STR)

How do we delete 'B' in given List colors?

  1. del colors[2]
  2. colors.remove("B")
  3. colors.pop(2)
  4. All of these
Answer

All of these

Reason — The statement STR = "RGBCOLOR" assigns the string "RGBCOLOR" to the variable STR, and colors = list(STR) converts this string into a list of its individual characters. As a result, colors will be ['R', 'G', 'B', 'C', 'O', 'L', 'O', 'R']. All of the methods mentioned can be used to delete 'B' from the list colors. Using del colors[2] removes the item at index 2, which is 'B'. The colors.remove("B") method removes the first occurrence of the value 'B'. Similarly, colors.pop(2) removes and returns the item at index 2, which is 'B'.

Get the Bright Tutorials app Stuck on a question? Ask Bright Buddy — your AI tutor — for step-by-step help in the app.