[Tutor] Newline issues

Alan Gauld alan.gauld at yahoo.co.uk
Mon May 11 19:21:42 EDT 2020


On 11/05/2020 19:36, boB Stepp wrote:

> Now we get to my point of confusion.  When I copy and paste the text.txt
> contents into a triple-quoted string variable in the interpreter I get
> results I am not sure I understand:
> 
> 3.7.5:  s = """
> ...     NOTE:This is a test contact to see all fields.\n2nd line\:  Did not fill in
> ...       \"Chat\" field.\n3rd line\:  Did not fill in \"Internet call\" field.\n4t
> ...      h line\:  I will keep typing until the editor flows this text to the follo
> ...      wing line.  It will now happen.\n1st custom label: My first custom field\n
> ...      2nd custom label: My second custom field\n3rd custom label: My third custo
> ...      m field
> ...     """
> 3.7.5:  s
> '\nNOTE:This is a test contact to see all fields.\n2nd line\\:  Did not fill in\n  "Chat" field.\n3rd line\\:  Did not fill in "Internet call" field.\n4t\n h line\\:  I will keep typing until the editor flows this text to the follo\n wing line.  It will now happen.\n1st custom label: My first custom field\n\n 2nd custom label: My second custom field\n3rd custom label: My third custo\n m field\n'
> 3.7.5:  print(s)
> 
> NOTE:This is a test contact to see all fields.
> 2nd line\:  Did not fill in
>   "Chat" field.
> 3rd line\:  Did not fill in "Internet call" field.
> 4t
>  h line\:  I will keep typing until the editor flows this text to the follo
>  wing line.  It will now happen.
> 1st custom label: My first custom field
> 
>  2nd custom label: My second custom field
> 3rd custom label: My third custo
>  m field
> 
> I am not certain how just entering "s" in the interpreter will display in
> your MUAs, but in my display it as if the natural line breaks have
> vanished with the exception of the manual one I entered immediately after
> the first triple-quote and what I entered just before the final
> triple-quote.

I think you are seeing the difference between str.__repr__() and
str.__str__()

It happens with double or soingle quoted strings too:

>>> print("this is a double\nline of text")
this is a double
line of text
>>> "this is a double\nline of text"
'this is a double\nline of text'
>>>

repr() just outputs the characters without taking heed of escape
characters like newlines whereas str() interprets escape characters.
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list