Courseiva
Strings →easyMultiple Choice

PCAP Strings Practice Question

A user entered a string ' Hello, World! '. Which expression returns 'Hello, World!'?

⚠ Common exam trap

Python Institute often tests the distinction between `strip()`, `lstrip()`, and `rstrip()` by presenting a string with both leading and trailing whitespace, tempting candidates to choose a partial removal method when only the full `strip()` works.

Answer choices

Why each option matters

Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.

Correct answer & explanation

✓

s.strip()

The `strip()` method removes all leading and trailing whitespace characters from a string, returning a new string without the surrounding spaces. In this case, `s.strip()` removes the three leading spaces and three trailing spaces from ' Hello, World! ', resulting in 'Hello, World!'.

Answer analysis

Option-by-option breakdown

For each option: why learners choose it and why it is or isn't the right answer here.

  • ✗

    s.split()

    Why it's wrong here

    split() is used to divide a string into a list of substrings based on a delimiter, defaulting to any whitespace. For the input "hello world", it returns the list ['hello', 'world'], not a single string. This method does not remove whitespace from the string; it uses whitespace as separators and discards them in the result. Therefore, it cannot produce a trimmed version of the original string and is incorrect for this purpose.

  • ✓

    s.strip()

    Why this is correct

    strip() removes all leading and trailing whitespace characters — such as spaces, tabs, and newlines — and returns a new string with those characters removed. For "hello world", there are no surrounding whitespace characters, so it returns the exact same string unchanged. It leaves any internal whitespace intact, which is the desired behavior for trimming a string. This method is the standard and correct way to clean up whitespace at both ends of a string.

  • ✗

    s.rstrip()

    Why it's wrong here

    rstrip() only trims whitespace from the right end of the string, leaving the left side completely untouched. If the original string had leading spaces, such as " hello world", rstrip() would return " hello world" — those leading spaces would still be present. Since the requirement is to remove whitespace from both ends, rstrip() is insufficient. It may be useful when only trailing whitespace matters, but it does not fully trim the string in general.

  • ✗

    s.lstrip()

    Why it's wrong here

    lstrip() operates only on the left side, removing leading whitespace while preserving any trailing whitespace. For example, "hello world " passed to lstrip() yields "hello world ", with the trailing spaces still in place. This one-sided behavior makes it incorrect when the goal is to eliminate whitespace from both ends of the string. It is the mirror image of rstrip() and similarly cannot serve as a general trim method.

About these practice questions

One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

JA

Written by Johnson Ajibi, MSc IT Security

Senior Network & Security Engineer · founder of Courseiva

This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.