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

时间:2018-10-11 17:46:06

标签: 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

1 个答案:

答案 0 :(得分:0)

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

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