Start learning free

LESSON 603 OF 673 · PROJECTS: COMMAND-LINE TOOLS

cat v3 in Python: Showing Tabs and Line Ends (-A)

Practice Read the full explanation

Now build the -A flag of the cat tool, as a new function. It makes whitespace visible: every tab character (\t) is replaced by the two characters ^I and every line ends with a literal $ marker (so trailing spaces become obvious). Lines are then joined with newlines.

Note: real cat -A also shows other control characters (such as ^M) and non-ASCII bytes; this version only handles tabs and line ends, like cat -T -E. A blank line becomes just $, and an empty list gives an empty string.

Function signature:

def showAllCat(self, lines: List[str]) -> str:

Example:

Input: lines = ["hello\tworld", "foo"]

Output: "hello^Iworld$\nfoo$"

When printed:
hello^Iworld$
foo$

Another example (a blank line, trailing spaces):

Input: lines = ["a\t", "", "x  "]

Output: "a^I$\n$\nx  $"

When printed:
a^I$
$
x  $

Two transformations per line: replace tabs with "^I", then append "$". After both, join all the transformed lines with "\n".

Python strings have a built-in replace:
line.replace("\t", "^I") + "$"
Append each transformed line to a list, then "\n".join(out).

The full solution:
class Solution:
    def showAllCat(self, lines):
        out = []
        for line in lines:
            out.append(line.replace("\t", "^I") + "$")
        return "\n".join(out)

STEP 1 OF 4
    Ctrl + Enter to check

    PYTHON · PROJECTS: COMMAND-LINE TOOLS

    Cat V3 in Python, explained

    Problem overview

    This version adds the -A flag of cat as a new method, showAllCat(self, lines). Its job is to make whitespace visible. Every tab character (\t) becomes the two characters ^I, and every line ends with a literal $ marker, so trailing spaces become obvious. The transformed lines are then joined with newlines.

    This lesson handles only tabs and line ends, the same as cat -T -E. The real cat -A also shows other control characters and non-ASCII bytes, which are out of scope here.

    For ["hello\tworld", "foo"] the result is 'hello^Iworld$\nfoo$', and printing it gives:

    hello^Iworld$
    foo$
    

    A second example has a line ending in a tab, a blank line and a line with trailing spaces. For ["a\t", "", "x "] the result is 'a^I$\n$\nx $':

    a^I$
    $
    x  $
    

    A blank line becomes just $, and an empty list gives an empty string.

    Approach: two changes per line

    Each line goes through the same two transformations, in this order: replace its tabs with ^I, then append $. Doing the replacement first matters little for the result, since the marker contains no tabs, but it keeps the idea clear: first change what is inside the line, then mark where it ends. After every line is transformed, the rows are joined with "\n", as in the earlier versions.

    Unlike version 2, nothing here needs the line's position, so the loop can go over the lines themselves.

    Step by step

    1. Create an empty list named out to collect the changed lines.
    2. Loop with for line in lines. Only the text of each line is needed, not its index.
    3. Append the transformed line: line.replace("\t", "^I") swaps every tab for ^I, and + "$" adds the end marker.
    4. Return the rows joined with a newline between each two. This line sits after the loop, at the method's indent.

    The solution in Python

    class Solution:
        def showAllCat(self, lines):
            out = []
            for line in lines:
                out.append(line.replace("\t", "^I") + "$")
            return "\n".join(out)
    

    The Python details that matter here:

    • replace returns a new string. Strings are immutable, so line.replace("\t", "^I") on its own line does nothing useful. After line = "a\tb" and a bare line.replace("\t", "^I"), line is still 'a\tb'. Use the returned value, as the solution does by appending it.
    • replace changes every occurrence. "a\tb".replace("\t", "^I") gives 'a^Ib', and a line with two tabs has both replaced. An optional third argument would limit the count, and it isn't used here.
    • "\t" is one character. The two-character text ^I is what replaces it. That is why a line's visible length changes.
    • The + joins two strings. Both sides are strings, so no conversion is needed.

    Edge cases and common mistakes

    • A blank line. "" has no tabs, and adding the marker leaves "$". showAllCat([""]) returns '$', not an empty string.
    • An empty list. There are no rows, so the join gives ''. A list with one blank line is different: it gives '$'.
    • Trailing spaces. They stay in the text and the marker lands after them: "x " becomes 'x $'. Seeing the gap before $ is the purpose of the flag.
    • Several tabs in a row. showAllCat(["\t\t"]) returns '^I^I$': every tab is replaced separately.
    • Ignoring the result of replace. Calling line.replace(...) without using what it returns leaves the tabs in place.
    • Putting the marker on the joined string. Adding "$" once at the very end marks only the last line. The marker belongs to each line, before the join.
    • Replacing the wrong character. The tab is "\t". Replacing a space or typing a literal ^I in the input does nothing useful.

    Testing

    • ["hello\tworld", "foo"] gives 'hello^Iworld$\nfoo$'.
    • ["a\t", "", "x "] gives 'a^I$\n$\nx $'.
    • [""] gives '$'.
    • ["\t\t"] gives '^I^I$'.
    • [] gives ''.
    Keep going with Python650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free