Bird
0
0

Which of the following is a correct way to check if an AI output contains a banned word in Python?

easy📝 Syntax Q12 of 15
Agentic AI - Agent Safety and Guardrails
Which of the following is a correct way to check if an AI output contains a banned word in Python?
Aif output_text.contains(banned_word):
Bif output_text == banned_word:
Cif output_text.index(banned_word):
Dif banned_word in output_text:
Step-by-Step Solution
Solution:
  1. Step 1: Recall Python syntax for substring check

    In Python, to check if a substring is in a string, use the 'in' keyword.
  2. Step 2: Evaluate options

    if banned_word in output_text: uses 'if banned_word in output_text:', which is correct. if output_text == banned_word: checks equality, not containment. if output_text.contains(banned_word): uses a method that doesn't exist in Python strings. if output_text.index(banned_word): uses index incorrectly and can cause errors.
  3. Final Answer:

    if banned_word in output_text: -> Option D
  4. Quick Check:

    Substring check in Python uses 'in' [OK]
Quick Trick: Use 'in' keyword to check substring in Python strings [OK]
Common Mistakes:
  • Using equality instead of containment
  • Using non-existent string methods
  • Using index without error handling

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Agentic AI Quizzes