Q:

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

belongs to collection: Python String Programs

0

Python 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.

Python 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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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]

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Print the reverse of a string that contains digits... >>
<< Replace a special string from a given paragraph wi...