Bird
0
0

Which of the following is the correct syntax to loop over each character in the string text?

easy📝 Syntax Q12 of 15
Python - For Loop
Which of the following is the correct syntax to loop over each character in the string text?
Afor char to text:<br> print(char)
Bfor char in range(text):<br> print(char)
Cfor char in text:<br> print(char)
Dfor i in text.length:<br> print(i)
Step-by-Step Solution
Solution:
  1. Step 1: Identify correct for loop syntax for strings

    In Python, to loop over characters in a string, use for variable in string:.
  2. Step 2: Check each option

    for char in text:
    print(char)
    uses correct syntax. The other options use invalid syntax like 'to' instead of 'in', range() on a string, or non-existent .length attribute.
  3. Final Answer:

    for char in text:
    print(char)
    -> Option C
  4. Quick Check:

    Correct for loop syntax over string is for char in text:
    print(char) [OK]
Quick Trick: Use 'for char in string:' to loop letters [OK]
Common Mistakes:
MISTAKES
  • Using range() on a string directly
  • Trying to use .length instead of len()
  • Using incorrect loop keywords

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes