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:
Step 1: Recall Python syntax for substring check
In Python, to check if a substring is in a string, use the 'in' keyword.
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.
Final Answer:
if banned_word in output_text: -> Option D
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
Master "Agent Safety and Guardrails" in Agentic AI
9 interactive learning modes - each teaches the same concept differently