Python Função len(): Como encontrar o comprimento da string
Python Função len()
len() é uma função integrada em python. Você pode usar len() para obter o comprimento de uma determinada string, array, lista, tupla, dicionário, etc. Você pode usar a função len para otimizar o desempenho do programa. O número de elementos armazenados no objeto nunca é calculado, então len ajuda a fornecer o número de elementos.
Sintaxe
len(value)
parâmetros
Valor: o valor fornecido cujo comprimento você deseja.
Valor de retorno
Ele retornará um valor inteiro, ou seja, o comprimento de uma determinada string, ou array, ou lista ou coleção.
Vários tipos de valores de retorno:
Cordas:
Ele retorna o número de caracteres em uma string, que inclui pontuação, espaço e todos os tipos de caracteres especiais. No entanto, você deve ter muito cuidado ao usar len de uma variável nula.
Vazio:
Vazio é uma segunda chamada de retorno que possui zero caracteres, mas é sempre Nenhum.
Coleções:
O len interno retorna o número de elementos em uma coleção.
Erro de tipo:
A função Len depende do tipo de variável passada para ela. Um Non-Type não possui nenhum suporte integrado.
Dicionário:
Para o dicionário, cada par é contado como uma unidade. No entanto, valores e chaves não são independentes.
Exemplo 1: Como encontrar o comprimento da string fornecida?
# testing len() str1 = "Welcome to Guru99 Python Tutorials" print("The length of the string is :", len(str1))
Saída:
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))
Saída:
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))
Saída:
The length of the tuple is 3
Exemplo 4: Como encontrar o comprimento do dicionário em 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))
Saída:
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))
Saída:
The length of the Array is 4
Resumo
- len() é um recurso embutido função em python. Você pode usar len() para obter o comprimento de uma determinada string, array, lista, tupla, dicionário, etc.
- Valor: o valor fornecido cujo comprimento você deseja.
- Valor de retorno a retorna um valor inteiro, ou seja, o comprimento de uma determinada string, ou array, ou lista, ou coleções.