Last answered:

08 Apr 2024

Posted on:

31 Mar 2024

0

Resolved: len() function arguments

Why len('s') works but len(2) not?

Does len() function takes only strings?

1 answers ( 1 marked as helpful)
Instructor
Posted on:

08 Apr 2024

0

Hi Meenakshi!

Thanks for reaching out!

The len() function in Python is designed to return the length of an object that has a length. This typically includes data types like strings, lists, tuples, dictionaries, and other iterable objects. When you use len('s'), it works because 's' is a string with a length of 1. However, len(2) does not work because integers do not have a length. The len() function cannot be applied to integers or other non-iterable objects as they do not have a length.

Hope this helps.

Best,

Ivan

Submit an answer