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代码,该代码打印字符串的每个字符的ASCII值 。 ASCII代表美国信息交换标准代码。 它为我们提供了代表字符的数值。 为了解决这个问题,我们将使用range()函数和ord()函数。 在这里,我们将使用简单的技术来解决此问题。 在解决此问题之前,我们将了解有关range()和ord()函数的一些知识 。
Python range() function
Python range()函数
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.
range()是Python中可用的内置函数。 简而言之,该范围允许它们在给定间隔内生成一系列数字。 此函数仅适用于整数,即整数。
Python ord() function
Python ord()函数
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.
Python中的ord()函数接受长度为1的字符串作为参数,并返回传递的参数的ASCII值。 例如, ord('a')返回整数97 。
Read more: ASCII – American Standard Code for Information Interchange.
阅读更多: ASCII –美国信息交换标准代码。
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]
翻译自: https://www.includehelp.com/python/find-the-ascii-value-of-each-character-of-the-string.aspx