PCEP Using `sep=` and `end=` in `print()` 3 — Questions and Answers
Question 1: What is the output of `print('Python', 3, sep='.')`?
- Python.3 (Correct answer)
- Python 3
- Python3
- Python.3.
Correct answer: Python.3
sep='.' is placed between the string 'Python' and the integer 3 after it is converted to its string representation.
Question 2: Which of the following will print three items on the same line separated by tabs?
- print('a', 'b', 'c', sep='\t')
- print('a', 'b', 'c', end='\t')
- print('a\tb\tc')
- Both A and C (Correct answer)
Correct answer: Both A and C
Both using `sep='\t'` and embedding `\t` directly in one string produce tab-separated output on one line.
Question 3: What is the result of: ``` print('A', end='B') print('C') ```
- ABC (Correct answer)
- A\nBC
- AB\nC
- A BC
Correct answer: ABC
The first print ends with 'B' instead of '\n', so 'C' from the second print immediately follows, producing 'ABC' on one line.
Question 4: What is the default value of the `sep=` parameter in Python's `print()` function?
- A single space ' ' (Correct answer)
- An empty string ''
- A comma ','
- A newline '\n'
Correct answer: A single space ' '
The default separator is a single space, which is why `print('a', 'b')` outputs `a b`.
Question 5: What does the following print? ``` print(*[1, 2, 3], sep='-') ```
- 1-2-3 (Correct answer)
- [1, 2, 3]
- 1 2 3
- 1-2-3-
Correct answer: 1-2-3
The `*` unpacks the list into three separate arguments, and `sep='-'` places a hyphen between each of them.
Question 6: Which statement correctly describes `end=` in `print()`?
- It sets the string appended after all arguments are printed (Correct answer)
- It sets the string placed between arguments
- It determines the encoding used
- It specifies the last argument to print
Correct answer: It sets the string appended after all arguments are printed
`end=` controls the string that is appended after all positional arguments have been printed, defaulting to '\n'.
Question 7: What is the output of: ``` print('1', '2', sep='0', end='0') ```
- 10200
- 1020 (Correct answer)
- 102
- 1 2 0
Correct answer: 1020
sep='0' places a '0' between '1' and '2' giving '102', then end='0' appends another '0' giving '1020'.
What is the output of `print('Python', 3, sep='.')`?