Find the ASCII value of each character of the string in Python

Here, we will learn how to find the ASCII value of each character of the given string in Python programming language? By Bipin Kumar Last updated : February 25, 2024

A string will be given by the user and we have to write Python code that prints the ASCII value of each character of the string. ASCII stands for the American Standards Code for Information Interchange. It provides us the numerical value for the representation of characters.

Problem statement

Given a string, write a Python program to find the ASCII value of each character of the string.

Finding the ASCII value of each character of the string

To solve this problem, we will use the range() function and ord() function. Here, we will use simple techniques to solve this problem. Before going to solve this problem, we will learn a little bit about the range() and ord() function.

The range() function

The range() is a built-in function available in Python. In simple terms, the range allows them to generate a series of numbers within a given interval. This function only works with the integers i.e. whole numbers.

The ord() function

The ord() function in Python accepts a string of length 1 as an argument and returns the ASCII value of the passed argument. For example, ord('a') returns the integer 97.

Read more: ASCII – American Standard Code for Information Interchange.

Python program to find the ASCII value of each character of the string

# initialize a string
s='Motihari'
ascii_codes=[] # to contain ASCII codes

# getting ASCII values of each character
# using ord() method and appending them
# to "A"
for i in range(len(s)): 
    ascii_codes.append(ord(s[i])) 

# printing the result    
print('The ASCII value of each character are:',ascii_codes)

Output

The ASCII value of each character are: [77, 111, 116, 105, 104, 97, 114, 105]

To understand the above program, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.