【问题标题】:Print variable in python without space or newline [duplicate]在没有空格或换行符的python中打印变量[重复]
【发布时间】:2010-10-02 17:32:52
【问题描述】:

打印一个没有换行符或空格的变量 python3 通过 print (x,end='') 如何在 python 2.5 中做到这一点

【问题讨论】:

    标签: python printing formatting python-2.x python-2.5


    【解决方案1】:

    sys.stdout.write(仅)写入不带换行符的字符串,除非指定。

    >>> x = 4
    >>> print x
    4
    >>> import sys
    >>> sys.stdout.write(str(x)) # you have to str() your variables
    4>>> # <- no newline
    

    【讨论】:

    • 我想打印一个变量而不是字符串!
    • @san8055:我不关注,我会插入一个例子。
    【解决方案2】:

    简单:

    print x,
    

    注意最后的逗号。

    【讨论】:

      猜你喜欢
      • 2010-10-04
      • 2015-04-24
      相关资源
      最近更新 更多