我的节目以这种方式打印:
Mesagge to binary:asd
01100001
01110011
01100100
但我需要以这样的形式打印消息(011000010111001101100100)
我一直在尝试一切我不知道该怎么做的事。
def string_a_binario(s):
for i in range (0, len(s)):
if s[i] == " " :
print("00100001")
elif s[i] == "!" :
print("00100001")
elif s[i] == "#" :
print("00100011")
elif s[i] == "$" :
print("00100100")
elif s[i] == "%" :
print("00100101")
elif s[i] == "&" :
print("00100110")
elif s[i] == "'" :
print("00100111")
elif s[i] == "(" :
print("00101000")
elif s[i] == ")" :
print("00101001")
elif s[i] == "*" :
print("00101010")
elif s[i] == "+" :
print("00101011")
elif s[i] == "," :
print("00101100")
elif s[i] == "-" :
print("00101101")
elif s[i] == "." :
print("00101110")
elif s[i] == "/" :
print("00101111")
elif s[i] == "0" :
print("00110000")
elif s[i] == "1" :
print("00110001")
elif s[i] == "2" :
print("00110010")
elif s[i] == "3" :
print("00110011")
elif s[i] == "4" :
print("00110100")
elif s[i] == "5" :
print("00110101")
elif s[i] == "6" :
print("00110110")
elif s[i] == "7" :
print("00110111")
elif s[i] == "8" :
print("00111000")
elif s[i] == "9" :
print("00111001")
elif s[i] == ":" :
print("00111010")
elif s[i] == ";" :
print("00111011")
elif s[i] == "<" :
print("00111100")
elif s[i] == "=" :
print("00111101")
elif s[i] == ">" :
print("00111110")
elif s[i] == "?" :
print("00111111")
elif s[i] == "@" :
print("01000000")
elif s[i] == "A" :
print("01000001")
elif s[i] == "B" :
print("01000010")
elif s[i] == "C" :
print("01000011")
elif s[i] == "D" :
print("01000100")
elif s[i] == "E" :
print("01000101")
elif s[i] == "F" :
print("01000110")
elif s[i] == "G" :
print("01000111")
elif s[i] == "H" :
print("01001000")
elif s[i] == "I" :
print("01001001")
elif s[i] == "J" :
print("01001010")
elif s[i] == "K" :
print("01001011")
elif s[i] == "L" :
print("01001100")
elif s[i] == "M" :
print("01001101")
elif s[i] == "Ñ" :
print("01001110")
elif s[i] == "O" :
print("01001111")
elif s[i] == "P" :
print("01010000")
elif s[i] == "Q" :
print("01010001")
elif s[i] == "R" :
print("01010010")
elif s[i] == "O" :
print("01001111")
elif s[i] == "P" :
print("01010000")
elif s[i] == "Q" :
print("01010001")
elif s[i] == "R" :
print("01010010")
elif s[i] == "S" :
print("01010011")
elif s[i] == "T" :
print("01010100")
elif s[i] == "U" :
print("01010101")
elif s[i] == "V" :
print("01010110")
elif s[i] == "W" :
print("01010111")
elif s[i] == "X" :
print("01011000")
elif s[i] == "Y" :
print("01011001")
elif s[i] == "Z" :
print("01011010")
elif s[i] == "[" :
print("01011011")
elif s[i] == " \ " :
print("01011100")
elif s[i] == "]" :
print("01011101")
elif s[i] == "^" :
print("01011110")
elif s[i] == "_" :
print("01011111")
elif s[i] == "`" :
print("01100000")
elif s[i] == "a" :
print("01100001")
elif s[i] == "b" :
print("01100010")
elif s[i] == "c" :
print("01100011")
elif s[i] == "d" :
print("01100100")
elif s[i] == "e" :
print("01100101")
elif s[i] == "f" :
print("01100110")
elif s[i] == "g" :
print("01100111")
elif s[i] == "h" :
print("01101000")
elif s[i] == "i" :
print("01101001")
elif s[i] == "j" :
print("01101010")
elif s[i] == "k" :
print("01101011")
elif s[i] == "l" :
print("01101100")
elif s[i] == "m" :
print("01101101")
elif s[i] == "n" :
print("01101110")
elif s[i] == "o" :
print("01101111")
elif s[i] == "p" :
print("01110000")
elif s[i] == "q" :
print("01110001")
elif s[i] == "r" :
print("01110010")
elif s[i] == "s" :
print("01110011")
elif s[i] == "t" :
print("01110100")
elif s[i] == "u" :
print("01110101")
elif s[i] == "v" :
print("01110110")
elif s[i] == "w" :
print("01110111")
elif s[i] == "x" :
print("01111000")
elif s[i] == "y" :
print("01111001")
elif s[i] == "z" :
print("01111010")
elif s[i] == "{" :
print("01111011")
elif s[i] == "|" :
print("01111100")
elif s[i] == "}" :
print("01111101")
elif s[i] == "~" :
print("01111110")
s = list(input("Message to binary:"))
string_a_binario(s)
答案 0 :(得分:3)
对于多行问题,如果在,
语句末尾加上逗号(print
),则不会打印换行符:
示例:
print 'Put these on',
print 'just one line',
print 'please'
打印:
Put these on just one line please
注意:这仍然会在每个项目后打印空格。如果这是不可接受的,请使用sys.stdout.write()
之类的input
。
有一种 很多 更好的方法来实现这一点,而不是硬编码每个案例! (我无法想象你花了多长时间写这个!)
提示:
ord
将整数格式化为二进制字符串bin
其他python提示:
无需将list
的结果转换为for i in range (0, len(s)):
if s[i] == " " :
...
- python中的字符串是可迭代的。
这不是C - 您不需要使用数组索引进行迭代。相反,您可以迭代序列中的每个项。在这里,这些项目是字符串的每个字符!
for x in s:
is x == " ":
...
可以替换为
{{1}}
答案 1 :(得分:1)
说我有两个字符串s1
和s2
:
import sys
sys.stdout.write(s1)
sys.stdout.write(s2)
sys.stdout.write('\n')
sys.stdout.flush()
然后s1和s2将打印在同一行。
希望有所帮助:)
答案 2 :(得分:0)
我只想解决你编写函数的方式,它可能会引入一些错误。例如,查看循环中的前两个if
语句,看起来空格和!
字符都输出00100001
,是否需要?这有点类似于你的功能,可能会发现一些人为错误。
def string_a_binario(s):
for i in range(len(s)):
sys.stdout.write("{0:08b}".format(ord(s[i])))
此代码也可根据需要水平写入。但是,它与所有输入的功能不同。例如,如果没有满足任何条件,则代码不会打印任何内容,而此函数可能会打印出您不能满足的条件。
在python 3中(也许以前的版本,我不确定)你可以告诉print不要包含换行符,这是该函数的另一个版本:
def string_a_binario(s):
for i in range(len(s)):
print("{0:08b}".format(ord(s[i])), end='')
这是python 3中的另一个版本,它在每个二进制表示周围放置一个括号:
def string_a_binario(s):
for i in range(len(s)):
print("[{0:08b}".format(ord(s[i])), end=']')