Bird
Raised Fist0

Which Python statement correctly verifies that a user's input does not contain any spaces?

easy📝 Syntax Q3 of Q15
Testing Fundamentals - Non-Functional Testing
Which Python statement correctly verifies that a user's input does not contain any spaces?
Aif ' ' not in user_input:
Bif user_input.contains(' '):
Cif user_input.indexOf(' ') == -1:
Dif user_input.has(' '):
Step-by-Step Solution
Solution:
  1. Step 1: Understand Python syntax for substring check

    In Python, the correct way to check if a substring is not present is using the 'not in' keyword.
  2. Step 2: Analyze each option

    if ' ' not in user_input: uses 'if ' ' not in user_input:', which is valid Python syntax.
    if user_input.contains(' '): uses 'contains', which is not a Python string method.
    if user_input.indexOf(' ') == -1: uses 'indexOf', which is JavaScript syntax.
    if user_input.has(' '): uses 'has', which is not a Python string method.
  3. Final Answer:

    if ' ' not in user_input: -> Option A
  4. Quick Check:

    Check Python substring syntax [OK]
Quick Trick: Use 'in' or 'not in' for substring checks in Python [OK]
Common Mistakes:
MISTAKES
  • Using JavaScript methods like contains or indexOf in Python
  • Confusing syntax for substring presence check

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Testing Fundamentals Quizzes