Select the correct output of the code : S = “text#next” print(S.strip(“t”))
The Python string method 'strip()' works in this example. Understand how it removes specified characters from the beginning and end of a string, with the output of the code 'S.strip('t')' being 'ext#nex'.

In this article
Select the correct output of the code:
Code:
S = "text#next"
print(S.strip("t"))
Options:
- (A) ext#nex
- (B) ex#nex
- (C) text#nex
- (D) ext#next
Answer: (A) ext#nex
Explanation:
The strip() method in Python is used to remove leading and trailing characters (specified in the argument) from a string. It does not affect characters in the middle of the string.
In this case, the string "text#next" has the characters "t" specified for removal:
S = "text#next"
strip("t") will remove the 't' characters from both the beginning and the end of the string.
Step-by-Step Execution:
1. The strip("t") function will remove "t" from the left (beginning) and right (end) of the string.
Original string: "text#next"
After strip("t"): "ext#nex"
Final Output:
The final output of the code is ext#nex.
Please log in to leave a comment.