Bird
0
0

Find the bug in this sanitization code:

medium📝 Debug Q7 of 15
Agentic AI - Agent Safety and Guardrails
Find the bug in this sanitization code:
def sanitize(text):
    return text.strip().replace("'", "\'")
AMissing strip() call
BUsing double quotes inside replace
CIncorrect escaping of single quote in replace
DNo error, function is correct
Step-by-Step Solution
Solution:
  1. Step 1: Analyze replace syntax

    The replace("'", "\'") is incorrect because "\'" is not a valid escape sequence in Python strings and will cause a syntax error or unintended behavior.
  2. Step 2: Correct escaping

    To replace a single quote with an escaped single quote, the replacement string should be "\\'" (double backslash) to produce a literal backslash followed by a single quote.
  3. Final Answer:

    Incorrect escaping of single quote in replace -> Option C
  4. Quick Check:

    Proper escaping requires double backslash [OK]
Quick Trick: Use double backslash to escape single quote in strings [OK]
Common Mistakes:
  • Misunderstanding that \\' is needed (it produces \\')
  • Thinking strip() is misplaced
  • Claiming double quotes cause syntax issues

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes