Reputation: 35
I want to make a program which replaces a series of ascii codes to a string. Example input:
strin1 = "65 66 67 68 69"
But every time I run the program the result of the last ascii number is not produced. It just displays (note the missing 69):
65
66
67
68
This is my code:
cyfry = str(raw_input("put numbers here: "))
str = ""
length1 = len(cyfry)
for a in range(0,length1,1):
if (cyfry[a] != " "):
str += cyfry[a]
else:
print str
str = ""
Why is not the last substring not printed?
Upvotes: 0
Views: 22
Reputation: 350272
Your code only prints something when it arrives at a space. As your input does not end in a space, it does not print what str
contains when you reach the end of the input.
A simple solution is to add print str
at the very end of your program (outside of the loop).
Note that you can use cyfry.split(" ")
to get the "words" into a list. And then you can use int()
to convert such a word into an integer. chr()
can be used to get the character for that numerical code. Finally, you can join those individual characters to a new string with ''.join()
:
cyfry = raw_input("put numbers here: ")
str = ''.join([chr(int(ch)) for ch in cyfry.split(" ")])
print str
Upvotes: 1