Language/Python
Python - len(), 넘겨진 값의 길이나 item의 수를 반환하는 함수
TechNote.kr
2017. 12. 28. 17:16
728x90
len
>>> len(s)
전달받은 object의 길이나 가지고 있는 item의 수를 돌려준다.
__builtin__ module에 포함된 function 이다.
예)
숫자를 넘겨주었을 경우
>>> a = 1 >>> len(a) Traceback (most recent call last): File "", line 1, in TypeError: object of type 'int' has no len()
문자열을 넘겨주었을 경우
>>> a = "abcde" >>> len(a) 5
리스트를 넘겨주었을 경우
>>> a = [1, 2, 3, 4] >>> len(a) 4
튜플을 넘겨주었을 경우
>>> a = (1, 2, 3, 4) >>> len(a) 4
딕셔너리를 넘겨주었을 경우
>>> a = {"a":1, "b":2, "c":3} >>> len(a) 3
집합을 넘겨주었을 경우
>>> a = {"a", "b", "c", "d", "e"} >>> a set(['a', 'c', 'b', 'e', 'd']) >>> len(a) 5
참고>
자료형
>>> help(len)
Help on built-in function len in module __builtin__:
len(...)
len(object) -> integer
Return the number of items of a sequence or mapping.
728x90