Given a string s = “Welcome”, which of the following code is incorrect?
(A) print (s[0])
(B) print (s.lower())
(C) s[1] = ‘r’
(D) print (s.strip())
Answers
(C) s[1] = ‘r’ is incorrect.
Explanation:
In Python, strings are immutable, so you can't change their characters in-place.
Answer:
The correct answer is option (C) s[1] = 'r'
Explanation:
Given string is s = "Welcome"
(A) print (s[0])
This command will print the first letter of the string s, i.e., 'W'.
Hence, this code is correct.
(B) print (s.lower())
lower() function gives the output of the string in the lowercase. So, print(s.lower()) will give output as 'welcome'.
Hence, this code is also correct.
(C) s[1] = 'r'
s[1] prints the second letter of the string s. Accordingly s[1] = 'e'. But it is given that s[1] = 'r'.
Hence, this code is incorrect.
(D) print(s.strip())
strip() function prints a copy of the string by removing leading and trailing characters.
print(s.strip()) prints 'Welcome'.
Hence, this code is also correct.
So, option (c) is the correct answer.
#SPJ2