Question #186788

a. Write a for loop that prints the ASCII code of each character in a string named S. Use the built-in function ord(character) to convert each character to an ASCII integer.

b. Next, change your loop to compute the sum of the ASCII codes of all characters in a string


Expert's answer

a.

s = input('Please input string S: ')
for x in s:
    print('symbol: ' + x + ', ASCII code: ' + str(ord(x)))

b.

s = input('Please input string S: ')
sum = 0
for x in s:
    sum += ord(x)
print(sum)
LATEST TUTORIALS
APPROVED BY CLIENTS