Print parameter end= does not execute code properly

I tried the following code to specify a space as the final string to print:
print(“Here,” end=" “)
However, when I pressed the Enter key on my laptop, this is what happened:
Here
To my understanding, the Interactive Mode was supposed to render a newline where I could key in:
print(“it is…”)
So the final code would look like this:
print(“Here,” end=” ")
print(“it is…”)
With the result being: Here it is…
Any suggestions will be welcome.

Thanks.

The REPL will execute your first print() line before you could enter the second one. If you want to execute them together, you can put them into a function:

def example():
    print("Here", end=" ")
    print("it is...")

example()

Then it will work as you expect. Alternatively, you could put them in some indented block so that you don’t get back to the REPL before executing both:

if True:
    print("Here", end=" ")
    print("it is...")

Note that this is only relevant in the interactive shell. If you have the two print() lines in a regular Python program, there will be no prompt to break them up.

1 Like

Thanks for the advice.

1 Like