Python len() Funksjon: Hvordan finne lengden på strengen
Python len() Function
len() is a built-in function in python. You can use the len() to get the length of the given string, array, list, tuple, dictionary, etc. You can use len function to optimize the performance of the program. The number of elements stored in the object is never calculated, so len helps provide the number of elements.
syntax
len(value)
Parametre
Verdi: den gitte verdien du vil ha lengden på.
Returverdi
Det vil returnere en heltallsverdi, dvs. lengden på den gitte strengen, eller matrisen, eller listen eller samlingene.
Ulike typer returverdier:
Strenger:
Den returnerer antall tegn i en streng, som inkluderer tegnsetting, mellomrom og alle typer spesialtegn. Du bør imidlertid være veldig forsiktig når du bruker len av en Null-variabel.
Tømme:
Tom er et andre returanrop som har null tegn, men det er alltid Ingen.
Collections:
Den innebygde len returnerer antall elementer i en samling.
TypeError:
Len-funksjonen avhenger av typen variabel som sendes til den. En Non-Type har ingen innebygd støtte.
Ordbok:
For ordboken regnes hvert par som én enhet. Verdier og nøkler er imidlertid ikke uavhengige.
Eksempel 1: Hvordan finne lengden på den gitte strengen?
# testing len()
str1 = "Welcome to Guru99 Python Tutorials"
print("The length of the string is :", len(str1))
Utgang:
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))
Utgang:
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))
Utgang:
The length of the tuple is 3
Eksempel 4: Hvordan finne lengden på ordboken i 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))
Utgang:
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))
Utgang:
The length of the Array is 4
Sammendrag
- len() is a built-in function in python. Du kan bruke len() for å få lengden på den gitte strengen, matrisen, listen, tuppelen, ordboken osv.
- Verdi: den gitte verdien du vil ha lengden på.
- Returverdi a returnerer en heltallsverdi dvs. lengden på den gitte strengen, eller matrisen, eller listen eller samlingene.
