Python len() 함수: 문자열의 길이를 찾는 방법

Python len() 함수

len()은 파이썬에 내장된 함수입니다. len()을 사용하여 주어진 문자열, 배열, 목록, 튜플, 사전 등의 길이를 가져올 수 있습니다. len 함수를 사용하여 프로그램 성능을 최적화할 수 있습니다. 객체에 저장된 요소 수는 계산되지 않으므로 len은 요소 수를 제공하는 데 도움이 됩니다.

통사론

len(value)

파라미터

가치관: 길이를 원하는 주어진 값.

반환 값

이는 정수 값, 즉 주어진 문자열, 배열, 목록 또는 컬렉션의 길이를 반환합니다.

다양한 유형의 반환 값:

문자열 :

구두점, 공백 및 모든 유형의 특수 문자를 포함하여 문자열의 문자 수를 반환합니다. 그러나 Null 변수의 len을 사용할 때는 매우 주의해야 합니다.

빈:

비어 있음은 문자가 XNUMX개인 두 번째 반환 호출이지만 항상 없음입니다.

컬렉션 :

len 내장은 컬렉션의 요소 수를 반환합니다.

유형오류:

Len 함수는 전달된 변수의 유형에 따라 달라집니다. Non-Type에는 기본 제공 지원이 없습니다.

사전:

사전의 경우 각 쌍은 하나의 단위로 계산됩니다. 그러나 값과 키는 독립적이지 않습니다.

예제 1: 주어진 문자열의 길이를 어떻게 찾나요?

# testing len() 
str1 = "Welcome to  Guru99 Python Tutorials"
print("The length of the string  is :", len(str1))

출력:

The length of the string  is : 35

Example 2: How to find the length of the list in python?

# to find the length of the list

list1 = ["Tim","Charlie","Tiffany","Robert"]

print("The length of the list is", len(list1))

출력:

The length of the list is 4

Example 3: How to find the length of a tuple in python

# to find the length of the tuple

Tup = ('Jan','feb','march')

print("The length of the tuple is", len(Tup))

출력:

The length of the tuple is 3

예제 4: 사전의 길이를 찾는 방법 Python?

# to find the length of the Dictionary

Dict = {'Tim': 18,'Charlie':12,'Tiffany':22,'Robert':25}

print("The length of the Dictionary is", len(Dict))

출력:

The length of the Dictionary is 4

Example 5: How to find the length of the array in python

# to find the length of the array

arr1 = ['Tim','Charlie','Tiffany','Robert']

print("The length of the Array is", len(arr1))

출력:

The length of the Array is 4

요약

  • len()은 내장되어 있습니다. 파이썬의 함수. len()을 사용하여 주어진 문자열, 배열, 목록, 튜플, 사전 등의 길이를 가져올 수 있습니다.
  • 값: 길이를 원하는 주어진 값입니다.
  • 반환 값은 정수 값, 즉 주어진 문자열, 배열, 목록 또는 컬렉션의 길이를 반환합니다.