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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

The original list is : ['Geeks', 'for', 'Geeks']
The summation of strings is : 13


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.