PCEP-30-02 · Question #262
Which of the following statements can be used to return the length of the given string str?
The correct answer is D. len(str). len(str) is the correct Python built-in function for returning the length of a string - it works on any sequence type (strings, lists, tuples) by passing the object as an argument. str.size() (A) is invalid in Python; .size() is a method on NumPy arrays, not strings. str.len_()…
Question
Options
- Astr.size()
- Bstr.len_()
- Csize(str)
- Dlen(str)
How the community answered
(19 responses)- A5% (1)
- B11% (2)
- D84% (16)
Explanation
len(str) is the correct Python built-in function for returning the length of a string - it works on any sequence type (strings, lists, tuples) by passing the object as an argument.
str.size() (A) is invalid in Python; .size() is a method on NumPy arrays, not strings. str.len_() (B) does not exist at all in Python - this is a fabricated method that will raise an AttributeError. size(str) (C) is also not a built-in Python function; size only exists as numpy.size(), which doesn't apply to plain strings.
Memory tip: In Python, len() is a function you call on the object, not a method the object calls on itself - think "len is the language's tool, not the string's." If you're coming from Java or C++, resist the urge to write str.length().
Community Discussion
No community discussion yet for this question.