Start learning free

LESSON 1 OF 69 · PROJECTS: COMMAND-LINE TOOLS

cat v1 in Python: Printing Lines with join

Practice Read the full explanation

Build a tiny version of the Unix cat command. Given a list of lines, return them joined by newline characters (\n) so they print one per line.

Lines can be blank, and the list can be empty (the result is then an empty string).

Function signature:

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

Example:

Input: lines = ["hello", "world", "foo"]

Output: "hello\nworld\nfoo"

When printed:
hello
world
foo

Think of the output as one big string where each input line is separated from the next by a newline character. You don't need to loop and concatenate manually — Python has a built-in helper for this.

Use the string method "\n".join(lines) — this glues every element of lines together with "\n" in between.

The full solution is a one-liner:
class Solution:
    def cat(self, lines):
        return "\n".join(lines)

STEP 1 OF 1
    Ctrl + Enter to check

    PYTHON · PROJECTS: COMMAND-LINE TOOLS

    Cat V1 in Python, explained

    Problem overview

    This is the first version of a small project: a tiny copy of the Unix cat command, which prints text one line per row. The method cat(self, lines) receives a list of strings and returns one string in which the lines are separated by newline characters (\n). Printing that string shows each original line on its own row.

    For ["hello", "world", "foo"] the result is 'hello\nworld\nfoo', and printing it gives:

    hello
    world
    foo
    

    Lines may be blank, and the list may be empty. An empty list gives an empty string.

    Approach: a separator goes between lines

    The result is built by putting "\n" between each two lines. The word "between" matters: with three lines there are two separators, not three, so the last line is not followed by a newline.

    A loop that adds each line plus a newline would leave one newline too many at the end, and you would need extra code to remove it. Python's string method join does exactly the right thing in one call. It is called on the separator and takes the list: "\n".join(lines).

    Step by step

    The lesson has a single step: inside cat, return the lines glued together with join, using "\n" as the separator. There is nothing to set up first, because join accepts the whole list and handles every case: one line, many lines, blank lines and an empty list.

    The solution in Python

    class Solution:
        def cat(self, lines):
            return "\n".join(lines)
    

    Some details of join that are easy to trip on:

    • The separator is the string you call the method on. The list goes inside the parentheses. Writing lines.join("\n") fails, because lists have no join method.
    • Every item must be a string. "\n".join([1, 2]) raises TypeError: sequence item 0: expected str instance, int found. Convert numbers with str first.
    • Strings are immutable. join builds and returns a new string, and the list is left as it was.
    • Joining is also the efficient way. Adding strings one at a time in a loop makes a new string on every step, while join builds the result once.

    Edge cases and common mistakes

    • An empty list. There is nothing to separate, so cat([]) returns ''.
    • A single line. There is no separator at all: cat(["x"]) returns 'x', with no newline.
    • Blank lines. An empty string is still a line. cat(["a", "", "b"]) returns 'a\n\nb', which prints with an empty row in the middle.
    • One blank line looks like no lines. cat([""]) and cat([]) both return ''. The result is the same string, so the two inputs can't be told apart from it.
    • A trailing newline. The result ends with the last line's text, not with \n. Adding + "\n" would make every result one character too long and fail the lesson's tests.
    • Returning versus printing. The method returns the string. It does not call print.
    • Passing a string instead of a list. "\n".join("abc") treats the string as a sequence of characters and returns 'a\nb\nc'. The method expects a list of lines.

    Testing

    These are the results of calling cat on a few inputs:

    • ["hello", "world", "foo"] gives 'hello\nworld\nfoo'.
    • ["a", "", "b"] gives 'a\n\nb'.
    • ["x"] gives 'x'.
    • [""] gives ''.
    • [] gives ''.

    The next version builds on this one.

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free