Computer Science
Identify the output of the following code snippet:
text = "PYTHONPROGRAM"
text = text.replace('PY', '#')
print(text)
- #THONPROGRAM
- ##THON#ROGRAM
- #THON#ROGRAM
- #YTHON#ROGRAM
Answer
#THONPROGRAM
Reason — The replace()
method in Python searches for a specified substring and replaces it with another. In the given code, the substring 'PY' in the string "PYTHONPROGRAM" is replaced with the character '#'. As a result, the string "PYTHONPROGRAM" becomes "#THONPROGRAM", and that's the output displayed.
Related Questions
State True or False:
The Python interpreter handles logical errors during code execution.
Which of the following expressions evaluates to False ?
- not(True) and False
- True or False
- not(False and True)
- True and not(False)
What is the output of the expression ?
country = 'International' print(country.split("n"))
('I', 'ter', 'atio', 'aI')
['I', 'ter', 'atio', 'al']
['I', 'n', 'ter', 'n', 'atio', 'n', 'al']
Error
What will be the output of the following code snippet ?
message = "World Peace" print(message[-2::-2])