在Python中查找字符串的每个字符的ASCII值

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

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值