Computer Science

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

Python String Manipulation

1 Like

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'.

Answered By

2 Likes


Related Questions