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…

  1. A.

    It returns a string after removing leading and trailing whitespace.

  2. B.

    It returns a list by splitting the string at a specified delimiter.

  3. C.

    It returns the result of raising one value to the power of another.

  4. 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

  1. Let s = " data science ". The value has leading spaces, an internal space, and trailing spaces.

  2. Calling s.strip() examines both ends and returns "data science"; the internal space remains.

  3. The variable s still 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.

Explore the full course: Rssb Senior Computer Instructor

Loading lesson…