打印字符串而不是列表中的名称(Python)

2024-05-15 08:37:05 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我的问题是:

a = "01000001"
b = "01000010"
c = "01000011"
# and so on...

code1 = "hello"
read1 = 0
list1 = []
while(read1 < len(code1)) :
    list1.append(code1[read1])
    read1 = read1 + 1
output1 = 0
while(output1 < len(list1)) :
    print(list1[output1], end="")
    output1 = output1 + 1

它将只打印出>>>hello

我怎样才能让python打印出字符串而不是名称,从而得到: 0100100001000101010001010100010101001111


Tags: and字符串名称hellolensoonend
1条回答
网友
1楼 · 发布于 2024-05-15 08:37:05

将字符串转换为二进制的简单方法

st = 'hello'
print(' '.join(format(ord(x), 'b') for x in st))

相关问题 更多 >