Python | Strings length summation
Sometimes we receive data in the container that we need to process to handle it further for some essential utility. The magnitude of amount of data sometimes becomes important and needs to be known. This article discusses the total length of list of strings. Let’s discuss certain ways in which this can be done.
Method #1 : Using sum() + list comprehension
The combination of these two functions can be used to perform this particular function. The sum function is used to find the summation of each string of list and list comprehension does the task of iteration.
# Python3 code to demonstrate # string lengths summation # using sum() + list comprehension # initializing list of tuples test_list = ['Geeks', 'for', 'Geeks'] # printing the original list print ("The original list is : " + str(test_list)) # using sum() + list comprehension # string lengths summation res = sum(len(i) for i in test_list) # printing result print ("The summation of strings is : " + str(res)) |
The original list is : ['Geeks', 'for', 'Geeks'] The summation of strings is : 13
Method #2 : Using join() + len()
The inbuilt functions of python can help to perform this particular task. The join function can be used to join all the strings together and len function takes their cumulative sum.
# Python3 code to demonstrate # string lengths summation # using sum() + list comprehension # initializing list of tuples test_list = ['Geeks', 'for', 'Geeks'] # printing the original list print ("The original list is : " + str(test_list)) # using sum() + list comprehension # string lengths summation res = len(''.join(test_list)) # printing result print ("The summation of strings is : " + str(res)) |
The original list is : ['Geeks', 'for', 'Geeks'] The summation of strings is : 13
Recommended Posts:
- Python | dtype object length of Numpy array of strings
- Python | Split list of strings into sublists based on length
- Python | Summation of Non-Zero groups
- Python | Column summation of tuples
- Python | Summation of list as tuple attribute
- Python | Position Summation in List of Tuples
- Python | Alternate element summation in list
- Python | Grouped summation of tuple list
- Python | Column summation in uneven sized lists
- Python | Remove empty strings from list of strings
- Python | Tokenizing strings in list of strings
- Python string length | len()
- Run Length Encoding in Python
- Python | Custom length Matrix
- Python | Find whether all tuple have same length
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



