Bird
0
0

Which of the following is the correct syntax for a method that returns the length of a string s?

easy📝 Syntax Q3 of 15
Python - Methods and Behavior Definition
Which of the following is the correct syntax for a method that returns the length of a string s?
Adef length(s): return s.len()
Bdef length(s): print(len(s))
Cdef length(s): return len(s)
Ddef length(s): len(s)
Step-by-Step Solution
Solution:
  1. Step 1: Recognize correct syntax for returning string length

    Python's built-in function len() returns length; must be returned by method.
  2. Step 2: Identify syntax errors in other options

    def length(s): print(len(s)) prints instead of returning; C uses invalid method call; D lacks return.
  3. Final Answer:

    def length(s): return len(s) -> Option C
  4. Quick Check:

    Use return with len() function [OK]
Quick Trick: Use return with len() to get string length [OK]
Common Mistakes:
  • Using print instead of return
  • Calling len as a method on string
  • Omitting return keyword

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes