Courseiva
Strings →hardMultiple Choice

PCAP Strings Practice Question

You are a network engineer troubleshooting a script that processes router configuration files. The script reads a configuration line from a file: 'interface GigabitEthernet0/1

ip address 192.168.1.1 255.255.255.0
 no shutdown'. The script needs to extract the interface name and IP address. The current code uses string split operations but fails when the line has extra spaces or tabs. For example, when the line is 'interface   GigabitEthernet0/1', the split returns ['interface', '', '', 'GigabitEthernet0/1'] and the script fails. You need to modify the script to robustly extract the interface name and IP address regardless of whitespace. Which approach should you take?

⚠ Common exam trap

Candidates often confuse `split()` with `split(' ')`, assuming they behave identically, but `split(' ')` only splits on a single space character and leaves empty strings for multiple spaces or tabs, while `split()` handles all whitespace and removes empties automatically.

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

✓

Use line.split() instead of line.split(' ').

`line.split()` without arguments splits on any whitespace (spaces, tabs, newlines) and automatically removes empty strings, making it robust against extra spaces or tabs. In contrast, `line.split(' ')` splits only on single space characters, leaving empty strings when multiple spaces or tabs are present. This behavior is defined by Python's string method documentation and is essential for parsing configuration files where whitespace is inconsistent.

Answer analysis

Option-by-option breakdown

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

  • ✗

    Manually iterate and concatenate characters until a space is found.

    Why it's wrong here

    Manually iterating character-by-character until the first space is an anti-pattern: it hard-codes a single ASCII space as the delimiter, ignores tabs or runs of whitespace, and forces you to manage index bounds and edge cases like a missing space by hand. Python's split() already encapsulates whitespace semantics, so this bespoke logic is both more verbose and more prone to off-by-one errors, especially if the line has leading spaces or multiple consecutive delimiters.

  • ✓

    Use line.split() instead of line.split(' ').

    Why this is correct

    Calling split() with no arguments (or None) splits on any sequence of whitespace—spaces, tabs, newlines—and automatically discards leading/trailing delimiters, so repeated spaces do not produce empty strings. For a line such as 'interface GigabitEthernet0/1' this returns ['interface', 'GigabitEthernet0/1'] with the interface name as the second token, regardless of how many spaces separate the words. This is the canonical, robust way to parse whitespace-delimited configuration output.

  • ✗

    Use line.split(' ') and then filter out empty strings.

    Why it's wrong here

    While you can salvage line.split(' ') by wrapping it in a filter (e.g., filter(None, ...)), doing so is needlessly explicit and still requires an extra pass over the result to remove empty strings caused by repeated spaces. More importantly, it remains incorrect for tabs or mixed whitespace because split(' ') treats only the literal space character as a delimiter, so a line containing a tab would yield a single unbroken string instead of two tokens.

  • ✗

    Use line.startswith('interface') to identify the line, then extract substring.

    Why it's wrong here

    Using startswith('interface') merely checks whether the line begins with that substring; it does not extract the name, so you would still have to manually slice or strip to isolate the interface identifier. That approach is fragile because it naively matches lines like 'interfaces' or 'interface-range' and requires hard-coded offsets that break when whitespace varies, all while duplicating the tokenization that split() already handles correctly.

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.