Courseiva

PCAP · topic practice

Strings practice questions

The Strings domain covers Python's immutable str type: indexing, slicing, concatenation, and the methods that inspect and transform text. Questions ask you to predict exact output from slicing expressions, choose the right method for a task, and apply format specifiers. Expect code-reading items where one character of difference in a slice bound or format spec changes the result.

Courseiva uses original exam-style practice questions designed for learning and revision. The goal is to understand the concepts, recognise exam patterns, and improve through explanations — not memorise copied exam dumps.

Editorial oversight:Johnson Ajibi· MSc IT Security, IEEE Senior Member
19 questionsDomain: Strings

What the exam tests

What to know about Strings

You must read a string expression and state its exact value, pick the correct method for a described task, and build a format specifier that matches a required width, alignment, and precision. The single most important thing: remember slice stop indices are exclusive and format precision follows the dot.

Slicing with start, stop, and step, including negative indices and omitted bounds

String methods such as isalnum, isalpha, isdigit, split, join, strip, and replace

The format() method and f-strings with width, alignment, precision, and type specifiers

Immutability of str and how operations like concatenation or replace return new strings

Watch out for

Common Strings exam traps

  • ▸Assuming the stop index in a slice is included; it is excluded, so s[0:6] returns six characters, not seven.
  • ▸Confusing isalnum with isalpha or isdigit; isalnum accepts letters and digits but rejects spaces and punctuation.
  • ▸Misreading format specifiers like {:10.2f}; the width comes before the dot and precision after, and alignment defaults to right for numbers.

Practice set

Strings questions

19 questions · select your answer, then reveal the explanation

Question 1hardmultiple choice
Read the full Strings explanation →

A log analysis script needs to extract all IP addresses from a string. The IPs are in dotted-decimal format. Which regex pattern will correctly extract them?

Question 2hardmultiple choice
Read the full Strings explanation →

Given the string s = 'Hello World!', which expression returns a list of characters?

Question 3easymultiple choice
Read the full Strings explanation →

Which of the following is the correct way to format a string with variables?

Question 4hardmultiple choice
Read the full Strings explanation →

Which of the following methods can be used to check if a string starts with a vowel?

Question 5mediummultiple choice
Read the full Strings explanation →

Given s = 'Hello\nWorld!\n\n\n\n\n\n', what is the length of string s?

Exhibit

Refer to the exhibit.

Error log:
```
Traceback (most recent call last):
  File "test.py", line 3, in <module>
    print(s[15])
IndexError: string index out of range
```

Code:
```python
s = "Python Programming"
print(s[15])
```

Which THREE of the following are valid ways to create a string in Python?

Question 7hardmultiple choice
Read the full Strings explanation →

What is the output of the following code?

import re

text = "Contact support@example.com for help or sales@example.org for sales." pattern = r'\b\w+@\w+\.\w{2,4}\b' result = re.findall(pattern, text)

print(result)

Exhibit

Refer to the exhibit.

```python
import re
pattern = r'\b\w+@\w+\.\w+\b'
text = 'Contact us: support@example.com or sales@example.org.'
matches = re.findall(pattern, text)
print(matches)
```

Drag and drop the steps to install a third-party package using pip in Python into the correct order.

Drag or tap steps into the slots.

Steps
Order
1Step 1
2Step 2
3Step 3
4Step 4
5Step 5
Question 9mediummultiple choice
Read the full Strings explanation →

A function receives a string and needs to return a new string with all vowels removed. Which code snippet accomplishes this efficiently?

Question 10hardmultiple choice
Read the full Strings explanation →

Refer to the exhibit. What is the output?

Exhibit

Configuration snippet:

import json
policy_string = '{"allow": true, "rate": 100}'
parsed = json.loads(policy_string)
print(parsed['rate'])
Question 11mediummultiple choice
Read the full Strings explanation →

A developer needs to extract the domain name (e.g., 'example.com') from an email address stored in the variable 'email'. The code currently uses `email.split('@')[1]`, which returns the domain part. However, it fails for addresses containing an '@' character in the local part (e.g., 'user@name@domain.com'). Which approach correctly extracts the domain assuming the email is valid?

Question 12mediummultiple choice
Read the full Strings explanation →

A developer encounters an error: `ValueError: Unknown format code 's' for object of type 'int'` when running `print("{0:s}".format(42))`. What is the problem and how should it be fixed?

Question 13mediummultiple choice
Study the full Python automation breakdown →

Given `s = 'Python'`, what is the result of `s[-4::-1]`?

Which TWO of the following expressions yield the substring 'Py' from the string s = 'Python'?

Question 15easymultiple choice
Read the full Strings explanation →

Refer to the exhibit. What is the output?

Exhibit

text = "  Python programming  "
result = text.strip()
print(len(result))
Question 16mediummultiple choice
Read the full Strings explanation →

Refer to the exhibit. What is printed?

Exhibit

name = "Alice"
age = 25
txt = f"{name:>10} is {age:03d} years old."
print(txt)
Question 17mediummultiple choice
Read the full Strings explanation →

A developer wrote a script that processes user input. The script expects a string containing a list of comma-separated values. The user enters "apple, banana, cherry, date". The script uses split() to separate the items. Which code correctly extracts the second item without leading/trailing spaces?

Question 18hardmultiple choice
Read the full Strings explanation →

In a data processing pipeline, a string variable 'text' contains the value "Hello\nWorld\r\n". The developer needs to count the number of lines in the text. Which expression returns the correct line count (treating \n and \r\n as line terminators)?

Question 19mediummultiple choice
Study the full Python automation breakdown →

A function is supposed to return True if a string contains only digits, and False otherwise. Which implementation uses a Python string method correctly?

Free account

Track your progress over time

Create a free account to save your results and see which topics improve across sessions.

Focused Strings sessions

Start a Strings only practice session

Every question in these sessions is drawn from the Strings domain — nothing else.

Related practice questions

Related PCAP topic practice pages

Move into related areas when this topic feels solid.

Frequently asked questions

What does the PCAP exam test about Strings?
You must read a string expression and state its exact value, pick the correct method for a described task, and build a format specifier that matches a required width, alignment, and precision. The single most important thing: remember slice stop indices are exclusive and format precision follows the dot.
How should I use these practice questions?
Select your answer before revealing the explanation. Then read why each option is right or wrong — this active recall approach builds retention far faster than re-reading notes.
Can I practise just Strings questions in a focused session?
Yes — the session launcher on this page draws every question from the Strings domain. Use a 10-question session first to gauge your baseline, then move to 20 or 30 once the weak spots are clear.
Where can I practise other PCAP topics?
Use the topic links above to move to related areas, or go back to the PCAP question bank to see all topics.
Are these real exam questions or dumps?
These are original practice questions written to test the same concepts the PCAP exam covers. They are not copied from any real exam or dump site.