Which statement correctly describes Python’s str.strip() method when it is…
2024
Which statement correctly describes Python’s str.strip() method when it is called without arguments?
Answer: A. It returns a string after removing leading and trailing whitespace. — ConceptPython strings are immutable, so a string method returns a new string instead of changing the original. When str.strip() is called without a chars…
- A.
It returns a string after removing leading and trailing whitespace.
- B.
It returns a list by splitting the string at a specified delimiter.
- C.
It returns the result of raising one value to the power of another.
- D.
It returns a string with the first character uppercase and the remaining characters lowercase.
Attempted by 328 students.
Show answer & explanation
Correct answer: A
Concept
Python strings are immutable, so a string method returns a new string instead of changing the original. When str.strip() is called without a chars argument, it removes whitespace from both ends; whitespace inside the string is preserved.
Application
Let
s = " data science ". The value has leading spaces, an internal space, and trailing spaces.Calling
s.strip()examines both ends and returns"data science"; the internal space remains.The variable
sstill contains the original value because strings are immutable.
Cross-check and contrast
Returning a list by a delimiter describes
str.split().Raising one value to a power describes
pow()or the**operator.Uppercasing the first character and lowercasing the rest describes
str.capitalize().
Therefore, the applicable description is that leading and trailing whitespace is removed and a string is returned.