Find length of each string in a list python
I'm just starting with programming. I have a list of a few strings and now I need to print the biggest (in length) one. So I first want to just print the lengths of elements. I was trying things like this: Show
So how do I do it?
codeape 95.4k24 gold badges151 silver badges180 bronze badges asked Oct 27, 2009 at 9:50
3
First of all you don't have a list, you have a tuple. this code will work for any sequence, however; both lists and tuples are sequences (as well as strings, sets, etc). So, the answered Oct 27, 2009 at 10:02
SilentGhostSilentGhost 294k64 gold badges301 silver badges291 bronze badges 0 To print the lengths of the elements:
I recommend you read some tutorial material, for instance http://docs.python.org/tutorial/ answered Oct 27, 2009 at 9:54
codeapecodeape 95.4k24 gold badges151 silver badges180 bronze badges 2
UPDATE: SilentGhost's solution is a lot nicer. answered Oct 27, 2009 at 10:05
Tarnay KálmánTarnay Kálmán 6,8605 gold badges43 silver badges56 bronze badges just ask for the max according to the length
answered Oct 27, 2009 at 10:01
chubchub 7576 silver badges10 bronze badges 0 For those who are here because they want to measure the lengths of all the elements in a sequence(list,tuple, etc) and return those lengths into another sequence (list, tuple etc), do this: TLDR
Longer explanation (from the inner brackets, moving outwards)
answered Dec 13, 2021 at 22:25
The following code will print the largest string in the set:
answered Oct 27, 2009 at 9:57 3 This code maps "len" function over the list, to get another list containing the length of every item.
answered Apr 24, 2015 at 8:03
First of all to find the length of strings you can define the function and use map function as below:
After that you can call the max function on len_words to find the maximum value of string from list.
answered Dec 29, 2020 at 14:35
How do I find the length of a string in a list Python?A list is identifiable by the square brackets that surround it, and individual values are separated by a comma. To get the length of a list in Python, you can use the built-in len() function. Apart from the len() function, you can also use a for loop and the length_hint() function to get the length of a list.
How do I find the length of a string in a list?The len() function for getting the length of a list. Python has a built-in function len() for getting the total number of items in a list, tuple, arrays, dictionary etc. The len() method takes an argument where you may provide a list and it returns the length of the given list.
Can you use LEN () on a list?The function len() is one of Python's built-in functions. It returns the length of an object. For example, it can return the number of items in a list. You can use the function with many different data types.
How does LEN () work in Python?Python len() Function
The len() function returns the number of items in an object. When the object is a string, the len() function returns the number of characters in the string.
|