By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

Typing \n in strings

P: n/a
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 ?
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"Edward Diener" <el******@earthlink.net> wrote in message
news:RG*******************@newsread2.news.atl.eart hlink.net...
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\nis\nthe\ntime" 'now\nis\nthe\ntime' print "now\nis\nthe\ntime" now
is
the
time s = 'now\nis\nthe\ntime'
s 'now\nis\nthe\ntime' print s

now
is
the
time
Jul 18 '05 #2

P: n/a
Edward Diener wrote:
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\nme' 'test\nme' print 'test\nme' 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:
print repr('test\nme')

'test\nme'

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

-Peter
Jul 18 '05 #3

P: n/a
Paul Watson wrote:
"Edward Diener" <el******@earthlink.net> wrote in message
news:RG*******************@newsread2.news.atl.eart hlink.net...
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\nis\nthe\ntime" 'now\nis\nthe\ntime' print "now\nis\nthe\ntime"

now
is
the
time


Right you are and my faux pas. I didn't notice the print statement in the
tutorial.
Jul 18 '05 #4

P: n/a
>>> a = "line1\nline2"
a # This prints repr(a) 'line1\nline2' print a # This prints a

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

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.