Skip to main content

/docs/images/banner.jpg

Python

String Sum and Average

Title: WAP to calculate sum and average of strings

# WAP to input string of numbers separated by spaces and print avg and sum

str = input("Enter numbers separated by spaces: ")
# belowline of code splits a string into a list of substrings and converts each substring to an integer, and returns the list of integers.
nums = list(map(int, str.split()))
print("Sum:", sum(nums))
print("Avg:", sum(nums)/len(nums))
# Output:
# Enter numbers separated by spaces: 10 20 30 40 50
# Sum: 150
# Avg: 30.0