[Tutor] Escape codes and 'return statements'

alan.gauld@bt.com alan.gauld@bt.com
Thu, 17 Aug 2000 11:37:16 +0100


> Hi this is Albert,
> 
>     I'm currently learning Python and I came across this 
> things called "escape codes". I understand fairly most of 
> them except for the escape codes ' \n ' and (linefeed???) and 
> ' \r ' (carriage return).

I assume you are confused about the difference?
Back in the mists of computing time(up until the 
late 1970's) computers had teletypes for input/output

Teletypes had rolls of paper on which the printing was 
done and you could issue a linefeed to move the roll 
up one line at a time. This was mechanically much more 
efficient than moving up and moving the carriage back 
each time so if you wanted 3 blank lines you issued 3 
linefeeds.

They also had carriage return to bring the printhead back 
to the beginning of the line. Thus typically the user would 
type(or the program would output) a carriage return followed 
by one or more line feeds.

These two motions got encapsulated in ASCII by the control 
chararacters ^J(linefeed) and ^M(carriage return/linefeed)

With the advent of VDUs the need for a separate linefeed 
diminished and mostly we just use carriage return/linefeed 
as a single 'newline' command.

Thus '\r' is a pure linefeed with no c/r and '\n' combines 
both motions.

I'm not sure if there is a character for c/r only (which 
can be usefgul for controlling old printers which can only 
use one color per line - multiple passes allows multiple 
colors in a line, for example)

Does that help in any way?
Mostly you just need to use '\n'....

Alan G.

>     Also, I kinda understand 'return' statements but I would 
> like to know on how and when to use them properly.Thanks in advance.

Oooh thats a religious question. :-)
Simply put return causes an immediate return from the current 
function. Purists like to see a single return statement per 
function. Those of us brought up on C tend to use them rather 
more liberally!

Alan G.