Convert String to ASCII Value in Python

In this post, we will develop a Python program to convert the string to ASCII values. Based on this program we will also develop a program to find the sum of ASCII values of string in Python.

ASCII stands for American Standard Code for Information Interchange. It was developed by the ANSI (American National Standards Institute) and it is used to interchange the information from a high-level language to low-level language. Machine or Computer understand only binary languages. So, the character data type represents integers. For example, the ASCII value of the letter ‘A’ is 65.

String to ASCII in Python

We will discuss how to convert each character in a string to an ASCII value. We are using the ord() function to convert a character to an integer (ASCII value). Which is a built-in function in Python that accepts a char (a string of length 1) as an argument and returns the Unicode code point for that character. We can use this function to find the ASCII value of any character.

# Python program to find ASCII value of each character in string

# take input
string = input("Enter any string: ")

# printing ascii value of each character
for i in range(len(string)):
    print("The ASCII value of character %c = %d" %(string[i], ord(string[i])))

Output:-

Enter any string: Python
The ASCII value of character P = 80
The ASCII value of character y = 121
The ASCII value of character t = 116
The ASCII value of character h = 104
The ASCII value of character o = 111
The ASCII value of character n = 110

ASCII Value of String in Python

This is yet another way to find the ASCII value of each character in a string. This is just shorthand to the above program in which we compact the code using list comprehension.

# Python program to find ASCII value of each character in string

# take input
string = input("Enter any string: ")

# find ascii value using list comprehension
ASCII_value = [ord(ch) for ch in string]

# printing ascii value of each character
print("The ASCII value of characters:", ASCII_value)

Output:-

Enter any string: List
The ASCII value of characters: [76, 105, 115, 116]

Sum of ASCII Values of String in Python

In the previous program, we will find the ASCII value of each character in a string but in this program, we will develop a Python program to find the sum of ASCII values of all characters in a string.

# Python program to find ASCII value of string

# take input
string = input("Enter any string: ")

# find ascii value of string
ASCII_value = sum(ord(ch) for ch in string)

# printing ascii value
print("The ASCII value of characters:", ASCII_value)

Output:-

Enter any string: ASCII_value
The ASCII value of characters: 997

Python Program to Find String to ASCII

We are using the sum() and map() function to find the sum of ASCII values of all characters in a string. map() is a built-in function that applies a function on all the items of an iterator given as input.

# Python program to find ASCII value of string

# take input
string = input("Enter any string: ")

# find ascii value of string
ASCII_value = sum(map(ord, string))

# printing ascii value
print("The ASCII value of characters:", ASCII_value)

Output:-

Enter any string: map function
The ASCII value of characters: 1220

Also See:- How to Check if Two Strings are equal in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *