liveBook · Manning

Tab character

As explained in The Quick Python Book, Fourth Edition

A tab character is a whitespace character that represents horizontal spacing in source text.

A tab character is whitespace used for horizontal spacing. In Python strings, write it as the escape sequence \t; when printed, the terminal renders it as a tab. section 4.6 section 6.3.3

Keep these distinctions in mind:

  • Inside a string, \t represents one tab character rather than the two visible characters backslash and t. section 4.6
  • Interactive evaluation displays special characters explicitly, while print() sends them to the terminal for rendering. section 6.3.3
  • Do not mix tabs and spaces for Python indentation: they may look aligned in an editor but map to different indentation levels. section 8.10
  • Use spaces for Python indentation when possible; if tabs are required, use them consistently within a block. section 8.10

Visible value versus rendered output

The string 'a\n\tb' contains a newline followed by a tab. Evaluating it interactively shows the escapes, whereas print('a\n\tb') displays the characters as separate lines with horizontal spacing before b. section 6.3.3

'a\n\tb'
 
'a\n\tb'
 
print('a\n\tb')
 
a
    b

Compare the explicit escape notation with the terminal rendering of the same string. section 6.3.3

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Defines \t and demonstrates rendering, then warns about mixed indentation without exploring tab-stop mechanics.
Examples
Uses 'a\n\tb' to contrast interactive evaluation with print() output and shows tabs mixed with spaces causing indentation errors.
What it assumes
Assumes basic Python strings and statements while teaching the \t escape and whitespace-related indentation caution.
What you can do after
Evaluate/understand tab escapes in strings and avoid inconsistent tab-based Python indentation.
How it connects
Connects escape characters, strings, print(), indentation, and whitespace-sensitive blocks.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.