Home » Python » Python programs

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?
Submitted by Bipin Kumar, on December 06, 2019

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

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.

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

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]






Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.





Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.