f



print() with no newline

In this snippet:
  import sys
  PY3 = (sys.version_info[0] >= 3)

  def print_no_nl (s):
    if PY3:
      print (s, end="")
    else:
      print (s),

I'm getting a syntax error in Python2. Python3 is fine.
How can I make this Py2+3 compatible?
0
ElChino
12/18/2016 12:25:21 PM
comp.lang.python 77058 articles. 3 followers. Post Follow

2 Replies
77 Views

Similar Articles

[PageSpeed] 24

On 18 December 2016 at 13:25, ElChino <elchino@cnn.cn> wrote:
> In this snippet:
>   import sys
>   PY3 = (sys.version_info[0] >= 3)
>
>   def print_no_nl (s):
>     if PY3:
>       print (s, end="")
>     else:
>       print (s),
>
> I'm getting a syntax error in Python2. Python3 is fine.
> How can I make this Py2+3 compatible?

With a __future__ import, the Python 3 syntax will work with both Pythons:

from __future__ import print_function
print(s, end="")

-- 
Chris Warrick <https://chriswarrick.com/>
PGP: 5EAAEA16
0
Chris
12/18/2016 12:38:47 PM
Chris Warrick wrote:

>> I'm getting a syntax error in Python2. Python3 is fine.
>> How can I make this Py2+3 compatible?
> 
> With a __future__ import, the Python 3 syntax will work with both Pythons:
> 
> from __future__ import print_function
> print(s, end="")

Thanks. Lovely.


0
ElChino
12/18/2016 1:11:17 PM
Reply: