What will be the output of the following Python code? [code]re.sub('morning', 'evening', 'good morning')[/code]

Thanh Hoan

New member
What will be the output of the following Python code?
Code:
re.sub('morning', 'evening', 'good morning')
A. ‘good evening’
B. ‘good’
C. ‘morning’
D. ‘evening’
 
Answer: a
Explanation: The code shown above first searches for the pattern ‘morning’ in the string ‘good morning’ and then replaces this pattern with ‘evening’. Hence the output of this code is: ‘good evening’.
 

Tìm kiếm

Back
Top