AlgoMaster Logo

Strings - Quiz

Last Updated: January 3, 2026

Strings Exercises

31 quizzes

1
Code Completion

Complete the code so that it takes the last 3 characters from the variable text and prints them.

python
1
print(text[:])

Click an option to fill the blank:

2
Code Completion

Complete the code to split the variable sentence into words using spaces and count how many words there are.

python
1
word_count = len(sentence.(' '))

Click an option to fill the blank:

3
Code Completion

Complete the code to create a raw string for a regular expression pattern that matches a digit followed by a word character.

python
1
pattern = "\d\w"

Click an option to fill the blank:

4
Multiple Choice

Which statement about Python strings is TRUE?

5
Multiple Choice

Given s = "Hello", what does s[1] return?

6
Multiple Choice

Which slice returns the substring "World" from msg = "Hello World!"?

7
Multiple Choice

Which method removes whitespace from both ends of a string?

8
Multiple Choice

Which code correctly creates the string: He said "Hi"?

9
Multiple Choice

What is the main advantage of using join() over repeated concatenation with + for many substrings?

10
Multiple Choice

Which formatting style is generally recommended for new Python code because of clarity and performance?

11
Multiple Choice

Which raw string literal is valid in Python?

12
Multiple Choice

In a regular expression, what does the pattern ^[A-Z]+$ mean?

13
Multiple Choice

Given s = "python", which expression returns "pto"?

14
Multiple Choice

Which code uses str.format() to insert name and age into a sentence?

15
Sequencing

Order the steps to use a regex to find all email-like patterns in a text.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to create a formatted report line using an f-string.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this code?

1title = "Python Strings"
2print(len(title))
18
Output Prediction

What is the output of this code?

1s = "Hello, World!"
2print(s[-6:-1])
19
Output Prediction

What is the output of this code?

1text = "  Clean Me  "
2print(text.strip().upper())
20
Output Prediction

What is the output of this code?

1name = "Alex"
2age = 28
3print("{0} is {1} years old".format(name, age))
21
Output Prediction

What is the output of this code?

1pattern = r"\d+"
2text = "ID: 42"
3import re
4result = re.search(pattern, text)
5print(result.group())
22
Bug Spotting

Find the bug in this code that formats a message with an f-string.

Click on the line(s) that contain the bug.

python
1
user = "Sam"
2
score = 95
3
message = f"User: {user}, Score: {score} points"
4
print("message")
23
Bug Spotting

Find the bug in this code that extracts a file extension using slicing.

Click on the line(s) that contain the bug.

python
1
filename = "report.pdf"
2
extension = filename[-3:0]
3
print(extension)
24
Matching

Match each string method with what it does.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each regex token with its meaning.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to create a formatted log line that includes a level and a message using an f-string.

python
1
level = "INFO"
2
message = "Startup complete"
3
log_line =
4
print(log_line)

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the code to normalize a username by trimming whitespace and converting to lowercase.

python
1
raw_username = " AdminUser "
2
normalized = raw_username.().()
3
print(normalized)

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code to check if the given text starts with "Hello" in a case-insensitive way.

python
1
text = "Hello there"
2
check = text.().startswith()
3
print(check)

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the code to build a CSV line from three fields using join().

python
1
fields = ["Alice", "Developer", "Remote"]
2
line = .join()
3
print(line)

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that uses a raw string to define a regex pattern.

Click on the line to select.

python
1
import re
2
pattern = "\d+"  # pattern A
3
raw_pattern = r"\d+"  # pattern B
4
text = "Order 1234"
5
print(re.findall(raw_pattern, text))
31
Hotspot Selection

Click the line where an f-string is used.

Click on the line to select.

python
1
name = "Jordan"
2
role = "Engineer"
3
info = f"{name} - {role}"
4
print(info)