print parameters
- Python course
- Beginner
- Lesson 96/116
- 4 min
Learn output formatting with sep and end parameters.
print and the sep parameter
print() can take multiple values; by default it separates them with a space. The sep parameter changes this separator:
print("a", "b", "c") # a b c
print("a", "b", "c", sep="-") # a-b-c
print("a", "b", "c", sep="") # abcsep can be any string — dash, comma, empty string. The default is a space. This is handy for CSV lines and formatted output.
It is the flexible way to print many values on one line.
The end parameter
print() by default ends the line with \n. The end parameter changes this:
print("Loading", end="")
print("...", end="")
# Output: Loading...
print() # new lineWith end='' the print stays on the same line; this allows progress indicators and combined output. end can be any string.
Use end to merge multiple prints onto one line.
Practice Separate with dashes using sep
Write print("a", "b", "c", sep="-"). Then print print("bitti") on a new line.
Starter code
# join values with dash using sep
# then print bittiSample solution
print("a", "b", "c", sep="-")
print("bitti")Expected output
a-b-c bitti
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Match print parameters
| Term | Meaning |
|---|---|
sep |
Separator between values |
end |
Line terminator |
space |
sep default |
"\n" |
end default |
2 Complete the sep parameter
print("a", "b", sep=____)
print("a", "b", sep="-")
Explanation: sep="-" joins values with a dash.
3 What is the output of print('x', 'y', sep='')?
- x y
- xy Correct answer
- x,y
- x\ny
Explanation: With sep='' (empty string) there is nothing between the values.