Home > Blockchain >  How do I get rid of the spaces between the characters in the output using a print(), statement in a
How do I get rid of the spaces between the characters in the output using a print(), statement in a

Time:04-01

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.

CodePudding user response:

Instead of printing each character, you could first make a string of all the elements and then print the result.

print(''.join(map(str, [chr(c) for c in [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]])))

Try to find out what ''.join(values) does and what map(str, values) does.

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!

CodePudding user response:

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)

CodePudding user response:

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='') #<<

CodePudding user response:

Maybe print(''.join([chr(c) for c in secretMessageList]))

  • Related