Print and sum each element in a array in Python

This is the most pythonic solution I can think of; Use string formatting instead of the + operator when using strings – it’s more efficient and a good habit to get into, use map to remap the integers to strings and str.join to efficiently create the question string.

numbers = [1, 2, 3, 4]
print('{} = {}'.format('+'.join(map(str, numbers)), sum(numbers)))

Leave a Comment