Typing \n in strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Edward Diener

    Typing \n in strings

    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 ?


  • Paul Watson

    #2
    Re: Typing \n in strings

    "Edward Diener" <eldiener@earth link.net> wrote in message
    news:RGKbc.1113 0$yN6.10939@new sread2.news.atl .earthlink.net. ..[color=blue]
    > 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[/color]
    a[color=blue]
    > 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 ?
    >[/color]

    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.[color=blue][color=green][color=darkred]
    >>> "now\nis\nthe\n time"[/color][/color][/color]
    'now\nis\nthe\n time'[color=blue][color=green][color=darkred]
    >>> print "now\nis\nthe\n time"[/color][/color][/color]
    now
    is
    the
    time[color=blue][color=green][color=darkred]
    >>> s = 'now\nis\nthe\n time'
    >>> s[/color][/color][/color]
    'now\nis\nthe\n time'[color=blue][color=green][color=darkred]
    >>> print s[/color][/color][/color]
    now
    is
    the
    time


    Comment

    • Peter Hansen

      #3
      Re: Typing \n in strings

      Edward Diener wrote:
      [color=blue]
      > 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 ?[/color]

      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:
      [color=blue][color=green][color=darkred]
      >>> 'test\nme'[/color][/color][/color]
      'test\nme'[color=blue][color=green][color=darkred]
      >>> print 'test\nme'[/color][/color][/color]
      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:
      [color=blue][color=green][color=darkred]
      >>> print repr('test\nme' )[/color][/color][/color]
      'test\nme'

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

      -Peter

      Comment

      • Edward Diener

        #4
        Re: Typing \n in strings

        Paul Watson wrote:[color=blue]
        > "Edward Diener" <eldiener@earth link.net> wrote in message
        > news:RGKbc.1113 0$yN6.10939@new sread2.news.atl .earthlink.net. ..[color=green]
        >> 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 ?
        >>[/color]
        >
        > 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.[color=green][color=darkred]
        >>>> "now\nis\nthe\n time"[/color][/color]
        > 'now\nis\nthe\n time'[color=green][color=darkred]
        >>>> print "now\nis\nthe\n time"[/color][/color]
        > now
        > is
        > the
        > time[/color]

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


        Comment

        • Jeff Epler

          #5
          Re: Typing \n in strings

          >>> a = "line1\nlin e2"[color=blue][color=green][color=darkred]
          >>> a # This prints repr(a)[/color][/color][/color]
          'line1\nline2'[color=blue][color=green][color=darkred]
          >>> print a # This prints a[/color][/color][/color]
          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

          Comment

          Working...