[Tutor] Tutor Digest, Vol 32, Issue 51

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Oct 13 18:33:35 CEST 2006


>> What makes you think so?
>> 
>> >>> s = 'l'
>> >>> s += '\n'
>> >>> s
>> 'l\n'
>> >>>
>> 
>> Seems to work.... '\n' is just a character like any other.
>
> The issue was to have a newline or return character after every string, 
> Here it is just iterpreted as \n alphabet., but not as a return 
> character.


Hi Meher,

Are you familiar with Python's rules for escaped character literals?

     http://docs.python.org/ref/strings.html

In regular character literals, '\n' is treated as the single newline 
character, and not as the literal sequence of a backslash and 'n'.  We can 
see this concretely:

#############
>>> len('\n')
1
#############


The backslashing notation is necessary in Python because we often need to 
talk about characters that are hard to type literally.  The escape 
characters provide us a coded way to talk about those characters.

If we did want to talk about the string that contains the "backslash" and 
"n" sequence, we have to be a little subtle:

##############
>>> len('\\n')
2
##############


Alan and I want to clarify that the way the newlines are being handled in 
the original program looks perfectly fine at the moment; I don't think 
that's the source of the problem.


More information about the Tutor mailing list