Python program to find the sum of Characters ascii values in String List

In this article, we will learn a Python program to find the sum of characters' ASCII values in a list of strings. We'll explore two approaches: using loops and using list comprehension with built-in functions.

Problem Overview

Given a list of strings, we need to calculate the sum of ASCII values for each string's characters and return a list of these sums.

Example

For the input list ['hello', 'tutorialspoint', 'python', 'platform'], we calculate the ASCII sum for each word. For "hello", the ASCII values are h(104) + e(101) + l(108) + l(108) + o(111) = 532. However, in our examples, we subtract 96 from each ASCII value to get normalized values ?

Input

Input List: ['hello', 'tutorialspoint', 'python', 'platform']

Output

[52, 209, 98, 101]

Method 1: Using for Loop and ord() Function

This approach uses nested loops to iterate through each string and its characters ?

# input list
input_list = ["hello", "tutorialspoint", "python", "platform"]

# printing input list
print("Input List:", input_list)

# storing the total sum of ASCII values of all string elements of the list
result_list = []

# traversing through each element of an input list
for string in input_list:
   
   # initializing ASCII values sum as 0
   ascii_vals_sum = 0

   # traversing through each character of the current list element
   for char in string:
      
      # getting the ASCII value of the character using the ord() function and
      # adding it to the above ascii_vals_sum variable
      ascii_vals_sum += (ord(char) - 96)

   # appending ascii values sum to the resultant list
   result_list.append(ascii_vals_sum)

# printing list of the sum of characters ASCII values in an input list
print("List of the sum of characters ASCII values:", result_list)
Input List: ['hello', 'tutorialspoint', 'python', 'platform']
List of the sum of characters ASCII values: [52, 209, 98, 101]

Method 2: Using List Comprehension and sum() Function

This approach uses nested list comprehension for a more concise solution ?

# input list
input_list = ["hello", "tutorialspoint", "python", "platform"]

# printing input list
print("Input List:", input_list)

# Using nested list comprehension to traverse through the characters of each string
# Calculating resulting ASCII values and getting the sum using sum() function
result_list = [sum([ord(char) - 96 for char in string]) for string in input_list]

# printing list of the sum of characters ASCII values in an input list
print("List of the sum of characters ASCII values:", result_list)
Input List: ['hello', 'tutorialspoint', 'python', 'platform']
List of the sum of characters ASCII values: [52, 209, 98, 101]

Comparison

Method Readability Performance Best For
For Loop More explicit Slightly slower Beginners, debugging
List Comprehension More concise Faster Experienced users, cleaner code

Key Points

  • The ord() function returns the Unicode code point of a character

  • Subtracting 96 normalizes lowercase letters (a=1, b=2, etc.)

  • List comprehension provides a more Pythonic and efficient solution

  • Both methods handle strings of any length

Conclusion

Both methods effectively calculate ASCII sums for string lists. Use the for loop approach for better readability and debugging, or choose list comprehension for more concise and efficient code. The ord() function is essential for converting characters to their ASCII values.

---
Updated on: 2026-03-26T23:59:45+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements