nerdexam
Python_Institute

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

Which of the following statements can be used to return the length of the given string str?

Options

  • Astr.size()
  • Bstr.len_()
  • Csize(str)
  • Dlen(str)

How the community answered

(19 responses)
  • A
    5% (1)
  • B
    11% (2)
  • D
    84% (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.

Full PCEP-30-02 Practice