Typing \n in strings

E

Edward Diener

Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n in
string literals is the new-line character. I open the interpreter and type a
string with a \n in it but instead of outputting a string with a new line,
it outputs the \n as literal characters of the string. Is the tutorial
wrong, is the interpreter broken, or what is happening ?
 
P

Paul Watson

Edward Diener said:
Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n in
string literals is the new-line character. I open the interpreter and type a
string with a \n in it but instead of outputting a string with a new line,
it outputs the \n as literal characters of the string. Is the tutorial
wrong, is the interpreter broken, or what is happening ?

What exactly are you entering and what is output?

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
now
is
the
time
now
is
the
time
 
P

Peter Hansen

Edward said:
Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n in
string literals is the new-line character. I open the interpreter and type a
string with a \n in it but instead of outputting a string with a new line,
it outputs the \n as literal characters of the string. Is the tutorial
wrong, is the interpreter broken, or what is happening ?

Notice that there are also quotation marks around that string? What
you are seeing is _not_ the string itself, but a printable
representation of the string. Since it contains a newline, which is
not "printable" (by this definition, anyway) it converts it to \n
again. Trust that the string really does contain the ASCII LF
character (byte value 10) however. To prove it, and see the real
string, just print it:
test
me

Does that help?

Remember always that the interactive interpreter shows you the
expression using the repr() call, which with strings will give
you a representation that makes it clearer what non-printable
characters are contained therein. Here's something that might
clear up any remaining questions:
'test\nme'

(Or, it might just raise more questions in your mind...
experimenting a little more should answer most of them. :)

-Peter
 
J

Jeff Epler

a = "line1\nline2"line1
line2

When you enter an expression in the interactive interpreter, and its
result is not None, Python shows you the repr() of the value. When you
use 'print expression', you are shown the str() of the value.
Skip ahead to 7.1, "Fancier Output Formatting", for some more details.

Jeff
 
E

Edward Diener

Paul said:
Edward Diener said:
Python 2.3.3 on Win2K. In the Python tutorial it says that typing \n
in string literals is the new-line character. I open the interpreter
and type a string with a \n in it but instead of outputting a string
with a new line, it outputs the \n as literal characters of the
string. Is the tutorial wrong, is the interpreter broken, or what is
happening ?

What exactly are you entering and what is output?

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)]
on win32
Type "help", "copyright", "credits" or "license" for more information.now
is
the
time

Right you are and my faux pas. I didn't notice the print statement in the
tutorial.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top