#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    temporarly: Rosetown, Sk (Canada)
    Posts
    22
    Rep Power
    0

    Escape sequences in strings?


    Do escape sequences like '\t' (supposed to create a tab) or '\n' (supposed to promt the program to swich one line down) work in strings? If yes, how?
    I typed in:
    Code:
    print 'This '\t' does not '\n' '\t' work too well'
    And got an "invalid token" error message.
    Thanks for any help!
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    Bay area
    Posts
    9
    Rep Power
    0
    It works... it's just that '\t' isn't just an escape sequence, it's an escape sequence in quotes...

    try:
    Code:
    print 'This\twill\nwork'
  4. #3
  5. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Code:
    >>> print 'This\twill\nwork'
    This	will
    work
    >>>
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2005
    Location
    temporarly: Rosetown, Sk (Canada)
    Posts
    22
    Rep Power
    0
    Thanks a mil guys.
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Python doesn't allow you to use the same character(s) that you used to open the string (', ", ''' or """) inside a string without escaping the the character/character-sequence first. Your original code would work fine, you just need to do:

    Code:
    >>> print 'This\'\t\'will\'\n\'work'
    This'   'will'
    'work
    >>> print "This'\t'will'\n'work"
    This'   'will'
    'work
    >>> print """This       will
    ... also work!"""
    This    will
    also work!
    >>> print """This       \"""will
    ... \"""also work!"""
    This    """will
    """also work!
    >>>
    Mark .
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo