Python: Compute the sum of the ASCII values of the upper-case characters in a given string
Python Programming Puzzles: Exercise-22 with Solution
Write a Python program to compute the sum of the ASCII values of the upper-case characters in a given string.
Input: PytHon ExerciSEs Output: 373 Input: JavaScript Output: 157
Visual Presentation:
Sample Solution-1:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a string 'strs' as input
def test(strs):
# Use the 'filter' function to extract uppercase characters and 'map' to get their ASCII values
# Finally, calculate the sum of ASCII values of uppercase characters
return sum(map(ord, filter(str.isupper, strs)))
# Assign a specific string 'strs' to the variable
strs = "PytHon ExerciSEs"
# Print the original string 'strs'
print("Original strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Assign a different string 'strs' to the variable
strs = "JavaScript"
# Print the original string 'strs'
print("\nOriginal strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
Sample Output:
Original strings: PytHon ExerciSEs Sum of the ASCII values of the upper-case characters in the said string: 373 Original strings: JavaScript Sum of the ASCII values of the upper-case characters in the said string: 157
Flowchart:
Sample Solution-2:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a string 'strs' as input
def test(strs):
# Initialize a variable 'tot' to store the total ASCII value of uppercase characters
tot = 0
# Iterate through each character 'c' in the string 'strs'
for c in strs:
# Check if the character is uppercase using 'isupper()' method
if c.isupper():
# If uppercase, add its ASCII value to 'tot'
tot += ord(c)
# Return the total ASCII value of uppercase characters
return tot
# Assign a specific string 'strs' to the variable
strs = "PytHon ExerciSEs"
# Print the original string 'strs'
print("Original strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Assign a different string 'strs' to the variable
strs = "JavaScript"
# Print the original string 'strs'
print("\nOriginal strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
# Assign another string 'strs' to the variable
strs = "ARt"
# Print the original string 'strs'
print("\nOriginal strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
Sample Output:
Original strings: PytHon ExerciSEs Sum of the ASCII values of the upper-case characters in the said string: 373 Original strings: JavaScript Sum of the ASCII values of the upper-case characters in the said string: 157 Original strings: ARt Sum of the ASCII values of the upper-case characters in the said string: 147
Flowchart:
Sample Solution-3:
Python Code:
# License: https://bit.ly/3oLErEI
# Define a function named 'test' that takes a string 'strs' as input
def test(strs):
# Initialize a variable 'tot' to store the total ASCII value of uppercase characters
tot = 0
# Iterate through each character 'c' in the string 'strs'
for c in strs:
# Check if the character is uppercase using 'isupper()' method
if c.isupper():
# If uppercase, add its ASCII value to 'tot'
tot += ord(c)
# Return the total ASCII value of uppercase characters
return tot
# Assign a specific string 'strs' to the variable
strs = "PytHon ExerciSEs"
# Print the original string 'strs'
print("Original strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the 'strs' string
print(test(strs))
# Assign a different string 'strs' to the variable
strs = "JavaScript"
# Print the original string 'strs'
print("\nOriginal strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
# Assign another string 'strs' to the variable
strs = "ARt"
# Print the original string 'strs'
print("\nOriginal strings:")
print(strs)
# Print a message indicating the operation to be performed
print("Sum of the ASCII values of the upper-case characters in the said string:")
# Print the result of the test function applied to the updated 'strs' string
print(test(strs))
Sample Output:
Original strings: PytHon ExerciSEs Sum of the ASCII values of the upper-case characters in the said string: 373 Original strings: JavaScript Sum of the ASCII values of the upper-case characters in the said string: 157 Original strings: ARt Sum of the ASCII values of the upper-case characters in the said string: 147
Flowchart:
Python Code Editor :
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Determine, for each string in a list, whether the last character is an isolated letter.
Next: Find the indices for which the numbers in the list drops.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/puzzles/python-programming-puzzles-22.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics