📖 Scenario: You are working on a simple program that manages a list of book titles in a small library. You want to practice handling string values and text operations in Python.
🎯 Goal: Build a program that stores book titles, sets a keyword to search for, finds all titles containing that keyword, and prints the matching titles.
📋 What You'll Learn
Create a list of book titles with exact given values
Create a keyword variable with a specific string
Use a list comprehension to find titles containing the keyword
Print the list of matching titles
💡 Why This Matters
🌍 Real World
Managing and searching text data like book titles, product names, or messages is common in many software applications.
💼 Career
Understanding string handling and filtering is important for roles in data processing, software development, and quality assurance.
Progress0 / 4 steps
1
Create the list of book titles
Create a list called books with these exact titles: 'The Great Gatsby', 'To Kill a Mockingbird', 'Great Expectations', 'The Catcher in the Rye', and 'The Grapes of Wrath'.
Python
Hint
Use square brackets [] to create a list and separate each title with commas.
2
Set the keyword to search
Create a variable called keyword and set it to the string 'Great'.
Python
Hint
Use simple assignment with the exact string 'Great'.
3
Find titles containing the keyword
Use a list comprehension to create a new list called matching_books that includes only the titles from books that contain the keyword string.
Python
Hint
Use [title for title in books if keyword in title] to filter titles containing the keyword.
4
Print the matching titles
Print the matching_books list to show the titles that contain the keyword.
Python
Hint
Use print(matching_books) to display the list.
Practice
(1/5)
1. What is the correct way to create a string in Python?
easy
A. Using quotes like "Hello" or 'Hello'
B. Using parentheses like (Hello)
C. Using curly braces like {Hello}
D. Using square brackets like [Hello]
Solution
Step 1: Understand string syntax in Python
Strings are text and must be enclosed in quotes, either single or double.
Step 2: Check each option
Only Using quotes like "Hello" or 'Hello' uses quotes correctly. Parentheses, curly braces, and square brackets are for other data types.
Final Answer:
Using quotes like "Hello" or 'Hello' -> Option A
Quick Check:
Strings need quotes = C [OK]
Hint: Remember: strings always need quotes around text [OK]
Common Mistakes:
Using parentheses instead of quotes
Confusing brackets with quotes
Forgetting to close quotes
2. Which of the following is the correct syntax to create a multi-line string in Python?
easy
A. '''This is a multi-line string'''
B. "This is a multi-line string"
C. 'This is a multi-line string'
D. --This is a multi-line string--
Solution
Step 1: Recall multi-line string syntax
Python uses triple quotes (either ''' or """) to create strings that span multiple lines.
Step 2: Evaluate options
'''This is a multi-line string''' uses triple single quotes correctly. Options B and C use single or double quotes for single-line strings. --This is a multi-line string-- is invalid syntax.
Final Answer:
'''This is a multi-line string''' -> Option A
Quick Check:
Triple quotes = multi-line string [OK]
Hint: Use triple quotes for multi-line strings [OK]
Common Mistakes:
Using single or double quotes for multi-line text
Using invalid symbols like --
Not closing triple quotes properly
3. What will be the output of the following code?
text = "Hello World"
print(text[6:])
medium
A. Hello
B. World
C. World!
D. Hello World
Solution
Step 1: Understand string slicing
text[6:] means start from index 6 to the end of the string. Indexing starts at 0.
Step 2: Identify substring from index 6
In "Hello World", index 6 is 'W', so text[6:] is "World".
Final Answer:
World -> Option B
Quick Check:
text[6:] = World [OK]
Hint: Remember string index starts at 0; slice from index 6 to end [OK]
Common Mistakes:
Confusing index start point
Including extra characters like '!'
Printing entire string instead of slice
4. Find the error in this code snippet:
name = 'Alice
print(name)
medium
A. Incorrect use of newline character
B. print() function is misspelled
C. Missing closing quote for the string
D. Variable name is invalid
Solution
Step 1: Check string syntax
The string starts with a single quote but does not have a closing quote before the newline.
Step 2: Identify syntax error
Because the closing quote is missing, Python will raise a syntax error.
Final Answer:
Missing closing quote for the string -> Option C
Quick Check:
Unclosed string causes syntax error [OK]
Hint: Always close your quotes before newline or end [OK]
Common Mistakes:
Forgetting to close quotes
Thinking newline character causes error
Assuming print is misspelled
5. You have a list of words: words = ['apple', 'banana', 'cherry']. Which code correctly joins them into a single string separated by commas?
hard
A. result = ','.join([words])
B. result = words.join(',')
C. result = ','.join('words')
D. result = ','.join(words)
Solution
Step 1: Understand join() method usage
The join() method is called on the string separator and takes an iterable of strings as argument.
Step 2: Check each option
result = ','.join(words) correctly calls join on the comma string with the list words. Others misuse join or pass wrong arguments.
Final Answer:
result = ','.join(words) -> Option D
Quick Check:
Separator.join(list) joins list items [OK]
Hint: Call join on separator string, pass list to join() [OK]
Common Mistakes:
Calling join on the list instead of the string
Passing string 'words' instead of list variable
Passing list inside another list
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.