Bird
Raised Fist0
DSA Pythonprogramming~10 mins

Substring Search Patterns in DSA Python - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to check if the pattern exists in the text using the 'in' keyword.

DSA Python
text = "hello world"
pattern = "world"
if pattern [1] text:
    print("Pattern found")
else:
    print("Pattern not found")
Drag options to blanks, or click blank then click option'
Anot in
Bin
C==
Dis
Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of 'in' to check substring.
Using 'is' which checks object identity, not substring.
2fill in blank
medium

Complete the code to find the first index of the pattern in the text using the find() method.

DSA Python
text = "abracadabra"
pattern = "cad"
index = text.[1](pattern)
print(index)
Drag options to blanks, or click blank then click option'
Afind
Bindex
Csearch
Dlocate
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' which raises an error if substring not found.
Using non-existent methods like 'search' or 'locate'.
3fill in blank
hard

Fix the error in the code to correctly count occurrences of the pattern in the text.

DSA Python
text = "banana"
pattern = "ana"
count = text.[1](pattern)
print(count)
Drag options to blanks, or click blank then click option'
Afind
Bindex
Ccount
Dsearch
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() which returns only the first index.
Using index() which raises error if substring not found.
4fill in blank
hard

Fill both blanks to create a dictionary of patterns and their counts from a list of words.

DSA Python
words = ["apple", "banana", "apple", "cherry"]
pattern_counts = {word: words.[1](word) for word in words if "a" [2] word}
print(pattern_counts)
Drag options to blanks, or click blank then click option'
Acount
Bfind
Cin
Dnot in
Attempts:
3 left
💡 Hint
Common Mistakes
Using find() instead of count() for counting.
Using 'not in' instead of 'in' to filter words.
5fill in blank
hard

Fill all three blanks to create a dictionary of uppercase patterns and their first index in text if found.

DSA Python
text = "hello world"
patterns = ["he", "wo", "or"]
result = {pattern.[1](): text.[2](pattern) for pattern in patterns if text.[3](pattern) != -1}
print(result)
Drag options to blanks, or click blank then click option'
Aupper
Bfind
Dcount
Attempts:
3 left
💡 Hint
Common Mistakes
Using count() instead of find() for index.
Using upper() incorrectly or missing it.

Practice

(1/5)
1. What does the in operator check when used like 'abc' in 'abcdef' in Python?
easy
A. If the substring 'abc' exists anywhere inside the string 'abcdef'
B. If the string 'abcdef' starts with 'abc'
C. If the string 'abcdef' ends with 'abc'
D. If the strings 'abc' and 'abcdef' are exactly equal

Solution

  1. Step 1: Understand the in operator for strings

    The in operator checks if the left string is found anywhere inside the right string.
  2. Step 2: Apply to given strings

    Since 'abc' is at the start of 'abcdef', it is found inside it.
  3. Final Answer:

    If the substring 'abc' exists anywhere inside the string 'abcdef' -> Option A
  4. Quick Check:

    'abc' in 'abcdef' = True [OK]
Hint: Remember: 'in' checks anywhere inside the string [OK]
Common Mistakes:
  • Thinking 'in' only checks start of string
  • Confusing 'in' with equality
  • Assuming 'in' checks only end of string
2. Which of the following is the correct syntax to find the index of substring 'cat' in string s = 'concatenate' using Python?
easy
A. s.indexOf('cat')
B. find(s, 'cat')
C. s.find('cat')
D. index(s, 'cat')

Solution

  1. Step 1: Recall Python string method for substring index

    Python strings have a method called find() to locate substring index.
  2. Step 2: Check method usage

    The correct syntax is s.find('cat'). Other options are invalid in Python.
  3. Final Answer:

    s.find('cat') -> Option C
  4. Quick Check:

    Use string.find(substring) syntax [OK]
Hint: Use string.find(substring) to get index [OK]
Common Mistakes:
  • Using non-Python syntax like indexOf
  • Calling find as a standalone function
  • Confusing method name or order of arguments
3. What will be the output of the following code?
text = 'abracadabra'
pos = text.find('ra')
print(pos)
medium
A. 2
B. 1
C. 3
D. -1

Solution

  1. Step 1: Understand find() method behavior

    The find() method returns the lowest index where the substring is found or -1 if not found.
  2. Step 2: Locate substring 'ra' in 'abracadabra'

    The substring 'ra' first appears starting at index 1 (abracadabra).
  3. Final Answer:

    1 -> Option B
  4. Quick Check:

    First 'ra' at index 1 [OK]
Hint: Remember: find() returns first occurrence index [OK]
Common Mistakes:
  • Assuming find() returns all occurrences
  • Confusing index with count
  • Mistaking find() return -1 for found substring
4. Identify the error in the following code snippet that tries to check if substring 'dog' is in string s:
s = 'hotdog'
if s.contains('dog'):
    print('Found')
else:
    print('Not Found')
medium
A. The method contains does not exist for strings in Python
B. The substring 'dog' is misspelled
C. The string s is not defined
D. The print statements are incorrect

Solution

  1. Step 1: Check string method availability in Python

    Python strings do not have a contains() method; membership is checked with in.
  2. Step 2: Correct way to check substring presence

    Use 'dog' in s instead of s.contains('dog').
  3. Final Answer:

    The method contains does not exist for strings in Python -> Option A
  4. Quick Check:

    Use in operator, not contains() [OK]
Hint: Use in for substring check, not contains() [OK]
Common Mistakes:
  • Using Java-like contains() method in Python
  • Misspelling substring or variable
  • Assuming print syntax is wrong
5. Given a string s = 'banana', which code snippet correctly finds all starting indices of substring 'ana' in s?
hard
A. indices = [s.find('ana')]
B. indices = [i for i in range(len(s)) if 'ana' in s[i]]
C. indices = [i for i in range(len(s)) if s.find('ana') == i]
D. indices = [i for i in range(len(s)) if s[i:i+3] == 'ana']

Solution

  1. Step 1: Understand substring search for all occurrences

    To find all starting indices, check each position if substring matches.
  2. Step 2: Analyze each option

    indices = [i for i in range(len(s)) if s[i:i+3] == 'ana'] uses slicing and compares substring at each index correctly. indices = [s.find('ana')] finds only first occurrence. indices = [i for i in range(len(s)) if s.find('ana') == i] incorrectly compares find() result to index. indices = [i for i in range(len(s)) if 'ana' in s[i]] incorrectly checks membership of substring in single character.
  3. Final Answer:

    indices = [i for i in range(len(s)) if s[i:i+3] == 'ana'] -> Option D
  4. Quick Check:

    Slice and compare at each index for all matches [OK]
Hint: Use slicing and loop to find all substring start indices [OK]
Common Mistakes:
  • Using find() only returns first match
  • Comparing find() result incorrectly
  • Checking substring in single character
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard