Sum of ASCII Values in String List using Python



In this article, we will learn a python program to find the sum of characters' ASCII values in a list of strings.

Methods Used

The following are the various methods to accomplish this task ?

  • Using for Loop, + operator, ord() function

  • Using list Comprehension, sum(), ord() functions

Example

Assume we have taken an input list containing string elements. We will find the @@

Input

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

Output

[52, 209, 98, 101]

Here, The ASCII values of all the characters of each list element like hello is 8+5+12+12+15 = 52 where ASCII (h) = 104 and starting ASCII value i.e ASCII (a) = 96 so 104-96 gives 8.

Method 1: Using for Loop, + operator, ord() function

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -.

  • Create a variable to store the input list and print the given list.

  • Create an empty list to store the total sum of ASCII values of all the string elements of the list.

  • Use the for loop, to traverse through each element of an input list.

  • Take a variable to store the ASCII values sum and initialize it to 0(asciiValsSum).

  • Use another nested for loop to traverse through each character of the current list element.

  • Get the ASCII value of the character using the ord() function(returns the Unicode code of a given character as a number) and add it to the above asciiValsSum variable.

  • Use the append() function(adds the element to the list at the end) to append ASCII values sum to the resultant list.

  • Print the list of sum of characters ASCII values in an input list.

Example

The following program returns the sum of characters ASCII values in a string list using for Loop, sum(), and ord() functions -

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

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

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

# traversing through each element of an input list
for i in inputList:
   
   # initializing ASCII values sum as 0
   asciiValsSum = 0

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

# appending ascii values sum to the resultant list
   resultList.append(asciiValsSum)

# printing list of the sum of characters ASCII values in an input list
print("List of the sum of characters ASCII values in an input list:", resultList)

Output

On executing, the above program will generate the following output -

Input List: ['hello', 'tutorialspoint', 'python', 'platform']
List of the sum of characters ASCII values in an input list: [52, 209, 98, 101]

Method 2: Using list Comprehension, sum(), ord() functions

List Comprehension

When you wish to build a new list based on the values of an existing list, list comprehension provides a shorter/concise syntax.

sum() function ? Returns the sum of all items in an iterable.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task -

  • Traverse in each string of the list of strings using list comprehension.

  • Use another nested list comprehension to traverse through the characters of the string.

  • Subtract the basic ASCII value (96) from each of the character ASCII values.

  • Get the sum of these ASCII values of characters using the sum() function.

  • Print the list of sum of characters ASCII values in an input list.

Example

The following program returns the sum of characters ASCII values in a string list using list comprehension, sum(), and ord() functions -

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

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

# Traversing in the given list of strings (input list)

# Using nested list comprehension to traverse through the characters of the string

# Calculating resulting ASCII values and getting the sum using sum() function
resultList = [sum([ord(element) - 96 for element in i]) for i in inputList]

# printing list of the sum of characters ASCII values in an input list
print("List of the sum of characters ASCII values in an input list:\n", resultList)

Output

On executing, the above program will generate the following output -

Input List: ['hello', 'tutorialspoint', 'python', 'platform']
List of the sum of characters ASCII values in an input list:
[52, 209, 98, 101]

Conclusion

In this article, we learned how to use 2 different methods to calculate the sum of a Character's ASCII values in a String List. In addition, we learned how to use nested listed comprehension rather than nested loops. Additionally, we learned how to use the ord() method to obtain a character's ASCII value.

Updated on: 2023-01-27T11:47:25+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements