Map function and Dictionary in Python to sum ASCII values Last Updated : 08 Feb, 2024 Comments Improve Suggest changes 6 Likes Like Report We are given a sentence in the English language(which can also contain digits), and we need to compute and print the sum of ASCII values of the characters of each word in that sentence. Examples: Input : GeeksforGeeks, a computer science portal for geeksOutput : Sentence representation as sum of ASCII each character in a word: 1361 97 879 730 658 327 527 Total sum -> 4579Here, [GeeksforGeeks, ] -> 1361, [a] -> 97, [computer] -> 879, [science] -> 730 [portal] -> 658, [for] -> 327, [geeks] -> 527 Input : I am a geekOutput : Sum of ASCII values: 73 206 97 412 Total sum -> 788This problem has an existing solution please refer to Sums of ASCII values of each word in a sentence link. We will solve this problem quickly in python using map() function and Dictionary data structures. Approach is simple. First split all words in sentence separated by space.Create a empty dictionary which will contain word as key and sum of ASCII values of it's characters as value.Now traverse list of splitted words and for each word map ord(chr) function on each character of current word and them calculate sum of ascii values of each character of current word.While traversing each word map sum of ascii values on it's corresponding word in resultant dictionary created above.Traverse splitted list of words and print their corresponding ascii value by looking up into resultant dictionary. Python3 # Function to find sums of ASCII values of each # word in a sentence in def asciiSums(sentence): # split words separated by space words = sentence.split(' ') # create empty dictionary result = {} # calculate sum of ascii values of each word for word in words: currentSum = sum(map(ord,word)) # map sum and word into resultant dictionary result[word] = currentSum totalSum = 0 # iterate list of splitted words in order to print # sum of ascii values of each word sequentially sumsOfAscii = [result[word] for word in words] print ('Sum of ASCII values:') print (' '.join(map(str,sumsOfAscii))) print ('Total Sum -> ',sum(sumsOfAscii)) # Driver program if __name__ == "__main__": sentence = 'I am a geek' asciiSums(sentence) Output: Sum of ASCII values:1361 97 879 730 658 327 527 Total sum -> 4579 Create Quiz Comment S Shashank Mishra Follow 6 Improve S Shashank Mishra Follow 6 Improve Article Tags : Python ASCII Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like