以下是Python中使用for循环输出字符串每个字节的示例代码:

string = "Hello, World!"
for byte in string.encode():
    print(byte)

输出结果为:

72
101
108
108
111
44
32
87
111
114
108
100
33

解释:

  • string是一个字符串变量。
  • string.encode()方法将字符串转换为字节数组,其中每个字符都被编码为一个字节。例如,字母'H'被编码为字节72。
  • for byte in string.encode():循环遍历字节数组中的每个字节。
  • print(byte)打印每个字节的值。
go循环输出字符串每个字节

原文地址: https://www.cveoy.top/t/topic/g5Oj 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录