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

Here, we will figure out how to discover the ASCII estimation of each character of the given string in Python programming language?

A string will be given by the client and we need to compose Python code that prints the ASCII estimation of each character of the string.

ASCII represents the American Standards Code for Information Interchange. It gives us a numerical incentive to the portrayal of characters. To tackle this issue, we will utilize the range() work and ord() work.

Here, we will utilize basic strategies to take care of this issue. Prior to going to take care of this issue, we will gain proficiency with a tad about the range() and ord() work.

Python range() function:

The range() is a worked in work accessible in Python. In straightforward terms, the range enables them to create a progression of numbers inside a given interim. This capacity just works with the whole numbers, for example, entire numbers.

Python ord() work:

The ord() work in Python acknowledges a string of length 1 as contention and returns the ASCII estimation of the passed contention. For example, ord(‘a’) restores the whole number 97.

Program:

# 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]

Leave a Comment

error: Alert: Content is protected!!