jmm37723
jmm37723

Reputation: 53

Python: Difference between typing a tab and \t in triple quotes

When writing a string in triple quotes """, what is the difference in literally putting in a \n or \t for new line or tab, and just writing it the way you want inside the quotes? Ex:

sample = """ I'm writing this 
On separate lines
    And tabs
So why can't I write it like this
/t instead of tabbing like this
\n or new lining like this.
Is one way preferred over the other? """

Is one way preferred?

Upvotes: 4

Views: 1488

Answers (4)

Michael Kropat
Michael Kropat

Reputation: 15227

As the other answers point out, \t is obvious what it is when skimming the code, so use it! Whereas you could easily mistake a literal tab for one or more spaces (especially if the tab character happens to occupy only a single character).

That's not to say you might never include literal tabs. Imagine you are embedding multi-line Makefile snippets in your Python source. The trick is that make(1) requires tabs. Which is clearer?

makefile = """
main.o : main.c defs.h
        cc -c main.c
kbd.o : kbd.c defs.h command.h
        cc -c kbd.c
command.o : command.c defs.h command.h
        cc -c command.c
"""

or

makefile = """
main.o : main.c defs.h
\tcc -c main.c
kbd.o : kbd.c defs.h command.h
\tcc -c kbd.c
command.o : command.c defs.h command.h
\tcc -c command.c
"""

Debatable, I suppose.

Abstract Syntax Trees

If you want to know what the literal difference is between two pieces of code, who knows better than Python itself? How do we do that? Well fortunately Python exposes a Python-language parser in the ast module:

>>> print('\\t')        # remember: we have to escape tab-escape with single quotes
\t
>>> import ast
>>> print(ast.dump(ast.parse('"""hello  world"""')))
Module(body=[Expr(value=Str(s='hello\tworld'))])
>>> print(ast.dump(ast.parse('"""hello\\tworld"""')))
Module(body=[Expr(value=Str(s='hello\tworld'))])
>>> ast.dump(ast.parse('"""hello        world"""')) == ast.dump(ast.parse('"""hello\\tworld"""'))
True

The parsed representation of both strings is the same, so as far as the Python interpreter is concerned, there's no difference between them.

Now contrast that with a raw string:

>>> print(ast.dump(ast.parse('r"""hello\\tworld"""')))
Module(body=[Expr(value=Str(s='hello\\tworld'))])

And we see that the representation is different (as expected).

Upvotes: 1

Maxime Lorant
Maxime Lorant

Reputation: 36181

There's no difference for the interpreter. The difference is for you and other programmers: with a \t you're sure that it's a tabulation. With a real tabulation in your file, it may be 4 spaces or a tabulation.

As @IanAuld said, and as described in the Zen of Python,

Explicit is better than implicit.

So I would prefer with \t.

Upvotes: 1

jfs
jfs

Reputation: 414675

tab is invisible so \t is preferable. But if you use raw string literal r"""\t""" then \t is two characters instead of one (meaning it is not a tab in this case). Also there is nothing specific about a tab inside triple quotes the behaviour is the same for ordinary quotes (both "" and '').

Upvotes: 1

kylieCatt
kylieCatt

Reputation: 11039

Remember it is better to be explicit rather than implicit. Using the specific \t and \n leaves no room for assumption.

Upvotes: 3

Related Questions