Courseiva
Strings →mediumMultiple Choice

PCAP Strings Practice Question

A programmer has a string s = 'Python programming is fun'. They want to extract the word 'programming'. Which slicing expression achieves this?

⚠ Common exam trap

The PCAP exam often tests the off-by-one error in slicing, where candidates forget that the stop index is exclusive, leading them to choose options that include an extra character or miss the correct substring.

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[7:18]

Python uses zero-based indexing. The word 'programming' starts at index 7 (the character 'p' in 'programming') and ends at index 17 (the character 'g'), but slicing is exclusive of the end index, so s[7:18] extracts characters from index 7 up to but not including index 18, which gives 'programming'.

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[6:18]

    Why it's wrong here

    This slice is incorrect because the start index 6 is the space character immediately before the 'p' of 'programming'. In Python's slice notation, the start index is inclusive, so index 6 is included in the result, yielding ' programming' (a leading space followed by the word). To capture only the word, the slice must begin at index 7, where the letter 'p' is located.

  • ✓

    s[7:18]

    Why this is correct

    This is the correct slice. Python's slice s[start:stop] includes characters from start up to, but not including, stop; therefore s[7:18] returns the characters at indices 7 through 17, which spell exactly 'programming'. The start index 7 points to the first letter 'p', and the exclusive stop index 18 excludes the space that immediately follows the word, ensuring a precise extraction.

  • ✗

    s[7:19]

    Why it's wrong here

    This slice is incorrect because, although the start index 7 correctly identifies the first letter 'p', the stop index 19 is exclusive, meaning characters up to index 18 are included. Since index 18 is the space after 'programming', the result is 'programming ' with a trailing space. The stop index must be 18, not 19, to exclude that boundary space.

  • ✗

    s[6:19]

    Why it's wrong here

    This slice is wrong for two reasons: the start index 6 includes the space before 'programming', and the stop index 19 includes the space after it. Because Python's slice includes the start index and excludes the stop index, this slice returns ' programming ' with a leading and trailing space. To extract just the word, the slice must begin at 7 and end at 18, excluding both surrounding spaces.

About these practice questions

Courseiva writes every PCAP question from scratch — 421 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. 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.