Weird newline character error in python

This question already has an answer here:

  • How to print without newline or space? 26 answers

  • This is because the print function automatically adds a newline character at the end (ascii 0x0a ). You can use the Python 3 version of print to set the end character:

    > python3 -c "print('x11x22x33x44', end='')" > new_file
    > hexdump -C new_file
    11 22 33 44
    

    If you really need to use Python 2, you can use this trick to run a multiline Python code in your terminal:

    echo -e "import sysnsys.stdout.write('x11x22x33x44')" | python > new_file
    
    链接地址: http://www.djcxy.com/p/77226.html

    上一篇: 如何在Python中将单个字符组合在一个字符串中

    下一篇: Python中奇怪的换行符错误