โ† All PCEP Flashcard Decks

Using `sep=` and `end=` in `print()` Flashcards

7 cards from real PCEP practice questions. Tap to flip, then mark Knew It or Still Learning โ€” missed cards come back until you master them.

Read the first 7 Using `sep=` and `end=` in `print()` flashcards as text
  1. What is the output of the following code? ```python print('apple', 'banana', 'cherry', sep=',') ```

    Answer: apple,banana,cherry

    The `sep` keyword argument specifies the separator to be used between the arguments of the `print()` function. By default, it's a space, but here it has been set to a comma, so the items are printed with commas between them.

  2. What is the output of this code snippet? ```python print('Hello', end=' ') print('World!') ```

    Answer: Hello World!

    The `end` keyword argument specifies what to print at the end of the line. By default, it's a newline character (`\n`). Here, it's set to a space for the first `print` call, so the second `print` call will start on the same line, immediately after the space.

  3. Which line of code will produce the output: `1-2-3`?

    Answer: print(1, 2, 3, sep='-')

    To place a hyphen between the arguments, you must specify `sep='-'`. The other options either use the default space separator, use the hyphen for the `end` argument, or result in a syntax error.

  4. What is the output of the following code? ```python for i in range(3): print(i, end='') ```

    Answer: 012

    The loop iterates for `i` values 0, 1, and 2. In each iteration, the `print` function is called with `end=''` which means no character (not even a space or newline) is printed at the end. This causes all the numbers to be printed consecutively on the same line.

  5. What are the default values for the `sep` and `end` arguments in the `print()` function?

    Answer: sep=' ' and end='\n'

    By default, the `print()` function separates its arguments with a single space (`' '`) and ends its output with a newline character (`'\n'`), which moves the cursor to the next line for subsequent output.

  6. What is the output of this code? ```python print(1, 2, 3, sep='*', end='\n---\n') print(4, 5, 6, sep='*') ```

    Answer: 1*2*3 --- 4*5*6

    The first `print` call separates the numbers 1, 2, 3 with asterisks and then ends with a newline, three hyphens, and another newline. The second `print` call then executes on a new line, separating 4, 5, 6 with asterisks and using the default `end` value (a single newline).

  7. How would you print the words 'one', 'two', and 'three' each on a new line using a single `print()` call?

    Answer: print('one', 'two', 'three', sep='\n')

    To print each argument on a new line, you need to change the separator between them from the default space to a newline character (`'\n'`). Using `sep='\n'` achieves this result with a single function call.

Using `sep=` and `end=` in `print()` Flashcards โ€” PCEP Study Cards with Answers