Posts: 18
Joined: Sat Apr 13, 2013 7:00 pm

PRINT function details

Sat Apr 13, 2013 7:51 pm

Hi, I'm new to the Pi but have quite a lot of experience of other computers and languages. This concerns the PRINT function in Python:
I'm using IDLE3 because as a newcomer I can't see the point of using earlier versions. I'm going through the tutorials in the MagPi, and using the Raspberry Pi User Guide too. But neither of these sources seem to contain the information I need.
I am trying to use the PRINT function to display messages on the screen, but have come across some annoying problems. The first is that PRINT always adds a space to whatever it has just outputted, so for instance if x=1 and y=2, I can't say:

print(x,y) because it comes out as 1 2 and not 12.

The User Guide says that the comma at the end ensures that strings are concatenated, but this doesn't work either. If I try to do the following:

print ("Foot"), on one line, and then, a few lines later, print("ball"),

It comes out as 'Foot' on one line and 'ball' on the next, no matter how many commas I use. I think I did have it print 'Foot ball' on one line at one time, but I have never seen 'Football' (all one word) yet.
This is pretty basic stuff, which I never had any trouble with before. In C there are various backslash commands to deal with this sort of thing, and in good ole' BBC BASIC there were things you could do with semi-colons, commas, and even nothing at all, after PRINT commands.

Where can I get information? I can't afford to keep spending a fortune on books that quickly go out of date and which are not available for viewing before buying to check for suitability. I want something like the BBC User Guide, with all the Python instructions listed, showing syntaxes and with an explanatory few paragraphs on each one. I've even searched this web site and found nothing.
In the meantime, could somebody out there give me a clue? How do I get rid of unwanted spaces and carriage returns? Its probably not rocket science...

Posts: 85
Joined: Sat Nov 05, 2011 11:31 pm

Re: PRINT function details

Sun Apr 14, 2013 11:50 am


Unfortunately this is one area where python 2.x and 3.x differ.
in python 3 if you want to suppress the newline you need to specify the "end" to use
>>> print("Foot",end="")
help tells us
>>> help(print)

Code: Select all

Help on built-in function print in module builtins:

    print(value, ..., sep=' ', end='\n', file=sys.stdout)
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file: a file-like object (stream); defaults to the current sys.stdout.
    sep:  string inserted between values, default a space.
    end:  string appended after the last value, default a newline.

Posts: 18
Joined: Sat Apr 13, 2013 7:00 pm

Re: PRINT function details

Sun Apr 14, 2013 1:12 pm

Thanks Tim, that's fantastic! I will now switch this silly PC off, switch the Pi on, and play with the print function. I didn't realise 'help' worked in IDLE. IDLE shows the syntax when I enter 'print' as a kind of tooltip, but not the full explanation. Which I wouldn't want anyway, now I know about 'help'. Thanks again!

Return to “Python”