Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Map function and Dictionary in Python to sum ASCII values
In this article, we are going to learn how to use the map() function along with dictionaries to sum the ASCII values of characters in strings.
The Python built-in ord() function returns the ASCII integer value of a character. Additionally, dictionaries (dict) in Python are used to store key-value pairs. We use them to associate strings with their total ASCII values, making it easy to store and retrieve results.
Python map() Function
The Python map() function applies a function to every item in an iterable and returns a map object. Here's the syntax ?
map(function, iterableObject)
Python ord() Function
The Python ord() function returns the Unicode code point of a character as an integer. The syntax is ?
ord(ch)
Summing ASCII Values of a String
We can combine map() with ord() and sum() to calculate the total ASCII value of all characters in a string ?
text = "Welcome"
result = sum(map(ord, text))
print("ASCII sum of 'Welcome':", result)
# Let's see individual ASCII values
ascii_values = list(map(ord, text))
print("Individual ASCII values:", ascii_values)
ASCII sum of 'Welcome': 716 Individual ASCII values: [87, 101, 108, 99, 111, 109, 101]
Using Dictionary to Store Multiple Results
We can use dictionary comprehension to store ASCII sums for multiple strings ?
car_brands = ["audi", "bmw", "ciaz"]
ascii_sums = {brand: sum(map(ord, brand)) for brand in car_brands}
print("ASCII sums:", ascii_sums)
# Adding more brands to the dictionary
ascii_sums["honda"] = sum(map(ord, "honda"))
ascii_sums["ford"] = sum(map(ord, "ford"))
print("Updated dictionary:", ascii_sums)
ASCII sums: {'audi': 419, 'bmw': 326, 'ciaz': 423}
Updated dictionary: {'audi': 419, 'bmw': 326, 'ciaz': 423, 'honda': 528, 'ford': 431}
Finding String with Highest ASCII Sum
You can find which string has the highest total ASCII value ?
words = ["python", "java", "javascript", "go"]
ascii_dict = {word: sum(map(ord, word)) for word in words}
# Find word with maximum ASCII sum
max_word = max(ascii_dict, key=ascii_dict.get)
print("ASCII sums:", ascii_dict)
print(f"'{max_word}' has the highest ASCII sum: {ascii_dict[max_word]}")
ASCII sums: {'python': 674, 'java': 410, 'javascript': 1064, 'go': 213}
'javascript' has the highest ASCII sum: 1064
Comparison Table
| Method | Use Case | Output |
|---|---|---|
sum(map(ord, string)) |
Single string ASCII sum | Integer |
{word: sum(map(ord, word)) for word in list} |
Multiple strings ASCII sums | Dictionary |
list(map(ord, string)) |
Individual ASCII values | List of integers |
Conclusion
The combination of map(), ord(), and sum() functions provides an efficient way to calculate ASCII sums. Using dictionaries allows you to store and compare results for multiple strings easily.
