Skip to content Skip to sidebar Skip to footer

New Line And Tab Characters In Python On Mac

I am printing a string to the python shell on a mac os 10.7.3. The string contains new line characters, \n\r, and tabs, \t. I'm not 100% sure what new line characters are used on e

Solution 1:

What look to you like newlines and carriage returns are actually two characters each -- a back slash plus a normal character.

Fix this by using your_string.decode('string_escape'):

>>> s = 'hello\\n\\r\\tworld'   # or s = r'hello\n\r\tworld'
>>> print s
hello\n\r\tworld
>>> print repr(s)
'hello\\n\\r\\tworld'
>>> print s.decode('string_escape')
hello
        world

Post a Comment for "New Line And Tab Characters In Python On Mac"