Reputation: 1
Here's the code that I have.
secretMessageList = [68, 111, 110, 116, 32, 100, 101, 99, 111, 100, 101, 32, 116, 104, 105, 115, 32, 109, 101, 115, 115, 97, 103, 101, 32, 111, 114, 32, 101, 108, 115, 101, 33]
for c in secretMessageList:
print chr(c),
And then the output looks like this.
D o n t d e c o d e t h i s m e s s a g e o r e l s e !
Is there any way to make the output instead look like this?
Dont decode this message or else!
I tried looking up if there's any way to do this but haven't found anything.
Upvotes: 0
Views: 107
Reputation: 1179
Instead of printing each character, you could first make a string of all the elements and then print the result.
print(''.join(map(chr, [68, 111, 110, 116, 32, 100, 101, 99, 111, 100, 101, 32, 116, 104, 105, 115, 32, 109, 101, 115, 115, 97, 103, 101, 32, 111, 114, 32, 101, 108, 115, 101, 33])))
map(chr, values)
has a result that is "almost the same" as [chr(c) for c in values]
. The returned value from map()
(map object) then can be passed to functions like list()
(to create a list), set()
(to create a set) or use it directly in join
.
A better readable solution would be
ints = [68, 111, 110, 116, 32, 100, 101, 99, 111, 100, 101, 32, 116, 104, 105, 115, 32, 109, 101, 115, 115, 97, 103, 101, 32, 111, 114, 32, 101, 108, 115, 101, 33]
characters = [chr(c) for c in ints]
string = ''.join(characters)
print(string)
In python 2.x, the print
method should be written without brackets, so you may need to remove those (because you tagged your question with python-2.7
). For Python 3 and up, print
does require brackets.
Happy coding, good luck!
Upvotes: 2
Reputation: 566
The simplest is just to change the character print
ends with:
secretMessageList = [68, 111, 110, 116, 32, 100, 101, 99, 111, 100, 101, 32, 116, 104, 105, 115, 32, 109, 101, 115, 115, 97, 103, 101, 32, 111, 114, 32, 101, 108, 115, 101, 33]
for c in secretMessageList:
print(chr(c), end='') #<<
Upvotes: 1
Reputation: 131
chr every item in your list then join them into string:
secretMessageList = [68, 111, 110, 116, 32, 100, 101, 99, 111, 100, 101, 32, 116, 104, 105, 115, 32, 109, 101, 115, 115, 97, 103, 101, 32, 111, 114, 32, 101, 108, 115, 101, 33]
a="".join(list(chr(i) for i in secretMessageList))
print(a)
Upvotes: 1