Use type() and isdigit() functions in Python to achieve a sum list of strings in Python. This function will check If the element is int, then add it to the total by checking two conditions.
Example sum list of strings in Python
lst = [1, '10', 'Hello', '2020', 'Python@2020', 2021] total = 0 # iterating over the list for element in lst: # checking whether its a number or not if isinstance(element, int) or element.isdigit(): # adding the element to the total total += int(element) print(total)
Or use List comprehension
total = sum([int(i) for i in lst if type(i)== int or i.isdigit()])
How to sum a list of string objects in Python?
Answer: Print only the first letter of each word in order to make one new word.
if words are separated by space, then split in words using space and for each word (“map” function), take the first character (x). Last join the result using void.
s = "this is my sentence" res = "".join(map(lambda x: x, s.split(" "))) print(res)
Or simple use
res = "".join(x for x in s.split())
Comment if you have any doubts or suggestions on this Python sum topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.