-4

In python 3, I'm tying to print a line without skipping a line as:

print('hello', end='')

And I'm getting this error message:

print('hello', end='')
                  ^
SyntaxError: invalid syntax

What's going on? How do I correct this error?

susan_rl
  • 1
  • 2
  • Note that I have consulted similar questions without a satisfactory results. – susan_rl Sep 14 '17 at 17:39
  • 2
    Are you sure you aren't using Python 2? Run `import sys; print(sys.version)` and see what it prints out. Post back with the results. – Christian Dean Sep 14 '17 at 17:40
  • @vaultah That question's answer did not solve my problem. – susan_rl Sep 14 '17 at 17:42
  • This looks like you're running python 2: try invoking with `python3` rather than `python`. You can check your version with `-V`: `python -V`. – Charlie Harding Sep 14 '17 at 17:43
  • 1
    what does `import sys; print(sys.version)` give you? – juanpa.arrivillaga Sep 14 '17 at 17:43
  • How are you running this line? Is it from the command line? Or via a script? What is the version of Python that you get from doing `python --version`? How about `import sys; print(sys.version)` in the shell or script? Please provide more info. – Mad Physicist Sep 14 '17 at 17:46
  • If you aren’t answering the question about version because you know you’re running python 3 so that’s not the problem, then consider 1. People will stop asking and try harder to figure out how this could happen if you just answer, and 2. If this is in python 3, there’s a good chance it’s an interpreter bug which may have been fixed at some point, so the version is still useful information. – Daniel H Sep 14 '17 at 17:49
  • @MadPhysicist: Probably shouldn't ask the OP to use `print()` to answer your question. Perhaps `import sys; sys.stdout.write(sys.version + "\n")`. – Steven Rumbalski Sep 14 '17 at 17:50
  • 2
    @StevenRumbalski The code `print(sys.version)` works for both function and statement `print`. It won’t work in python3 if some other function has been assigned to the name `print`, but that also wouldn’t generate this particular error. – Daniel H Sep 14 '17 at 17:54
  • @DanielH: And so it does. – Steven Rumbalski Sep 14 '17 at 17:54

1 Answers1

6

Are you absolutely sure you're using Python 3, and not accidentally invoking Python 2?

~ $ python3
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
hello>>>

~ $ python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print('hello', end='')
  File "<stdin>", line 1
    print('hello', end='')
                      ^
SyntaxError: invalid syntax
>>>
AKX
  • 152,115
  • 15
  • 115
  • 172