Free PCEP Using `sep=` and `end=` in `print()` Questions and Answers 1 — Questions and Answers
Question 1: What is the output of the following code? ```python print('apple', 'banana', 'cherry', sep=',') ```
- apple banana cherry
- apple,banana,cherry (Correct answer)
- ('apple', 'banana', 'cherry')
- apple, banana, cherry
Correct 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.
Question 2: What is the output of this code snippet? ```python print('Hello', end=' ') print('World!') ```
- Hello World!
- HelloWorld!
- Hello World! (Correct answer)
- A syntax error occurs.
Correct 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.
Question 3: Which line of code will produce the output: `1-2-3`?
- print(1, 2, 3)
- print(1, 2, 3, end='-')
- print(1, 2, 3, sep='-') (Correct answer)
- print(1-2-3)
Correct 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.
Question 4: What is the output of the following code? ```python for i in range(3): print(i, end='') ```
- 0 1 2
- 0 1 2
- 123
- 012 (Correct answer)
Correct 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.
Question 5: What are the default values for the `sep` and `end` arguments in the `print()` function?
- sep='\n' and end=' '
- sep='' and end='\n'
- sep=' ' and end='\n' (Correct answer)
- sep=' ' and end=''
Correct 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.
Question 6: What is the output of this code? ```python print(1, 2, 3, sep='*', end='\n---\n') print(4, 5, 6, sep='*') ```
- 1*2*3 --- 4*5*6 (Correct answer)
- 1*2*3--- 4*5*6
- 1*2*3 --- 4 5 6
- 1 2 3 --- 4*5*6
Correct 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).
Question 7: How would you print the words 'one', 'two', and 'three' each on a new line using a single `print()` call?
- print('one', 'two', 'three', end='\n')
- print('one\ntwo\nthree')
- print('one', 'two', 'three', sep='\n') (Correct answer)
- print('one') print('two') print('three')
Correct 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.
What is the output of the following code?
```python
print('apple', 'banana', 'cherry', sep=',')
```