#1
  1. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27

    escape sequences


    If i create the string "hello \nthere" the \n will ensure that "there" is displayed on the next line, but the \n will not actually be displayed.

    If i read text in from a text file how can i get python to display these escape sequences instead of say putting text on the next line?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    35
    Escape them.

    Code:
    print "hello \n there".replace('\n', '\\n')
  4. #3
  5. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    So i cant print it as

    'hello
    there'

    one minute and

    'hello \nthere'

    the next without changing the string?
  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    repr may be what you want...
    Code:
    >>> a = "hello\nworld"
    >>> print a
    hello
    world
    >>> b = repr(a)
    >>> print b
    'hello\nworld'
    >>>
    grim
  8. #5
  9. ID10T
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2004
    Location
    Yes
    Posts
    632
    Rep Power
    27
    Cheers

IMN logo majestic logo threadwatch logo seochat tools logo