如何在Python中省略换行符?

如何在Python中省略换行符?,python,Python,我知道。。。。。在Python中使用连接省略新行的一种方法: a = 'strin' b = 2 print str(b)+a 有多少种方法可以做到这一点?我相信,您正在使用Python2.x。您可以尝试以下操作: 使用尾随逗号 print a, # no new line will be printed 将来使用打印功能 from __future__ import print_function print(a,end='') # no new line will be p

我知道。。。。。在Python中使用连接省略新行的一种方法:

a = 'strin'  
b = 2  
print str(b)+a  

有多少种方法可以做到这一点?

我相信,您正在使用Python2.x。您可以尝试以下操作:

  • 使用尾随逗号

     print a, # no new line will be printed
    
  • 将来使用打印功能

    from __future__ import print_function
    print(a,end='') # no new line will be printed
    
  • 对于Python3.x,以下内容就可以了<代码>不需要导入打印功能

         print(a,end='')
    
    你也可以试试这个


    print(repr(b),a)#',将避免换行

    是的,我终于找到了答案。
    我们可以省略3中的换行符:

  • “+”(串联)
    例:

  • 通过使用“”,“(逗号)参见上述答案

  • 通过使用write()函数:

    import sys
    write = sys.stdout.write
    write('20)
    write('17')
    
    输出:

    2017
    
    write()
    方法不会在字符串末尾添加换行符('\n')


  • 谢谢你。。。但是,老兄,我正在使用3.5.2,谢谢。除此之外,
    打印(a,end=“”)
    将让您决定要用哪个字符结束输出。
    2017