The print() function shows messages or results on the screen. It helps you see what your program is doing.
print() function basics in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
print(value1, value2, ..., sep=' ', end='\n')
You can print one or more values separated by commas.
sep sets what goes between values (default is space). end sets what prints at the end (default is new line).
Examples
Python
print('Hello, world!')
Python
print('Hello', 'there')
Python
print('Hello', 'there', sep='-')
Python
print('Hello', end='!') print(' How are you?')
Sample Program
This program prints a name and age, then a welcome message with dashes, then a goodbye message followed by a blank line, and finally a see you soon message.
Python
name = 'Alice' age = 30 print('Name:', name) print('Age:', age) print('Welcome', name, 'to the program!', sep=' - ') print('Goodbye!', end='\n\n') print('See you soon!')
Important Notes
Use commas to print multiple items easily without converting to strings.
Remember print() adds a new line by default after printing.
You can change the end character to keep printing on the same line.
Summary
print() shows messages or values on the screen.
You can print many things at once separated by commas.
Use sep and end to control spacing and line breaks.
Practice
1. What does the
print() function do in Python?easy
Solution
Step 1: Understand the purpose of
Theprint()print()function is used to show messages or values on the screen, making it easy to see output from the program.Step 2: Compare with other options
Saving data, reading input, or creating variables are done by other functions, notprint().Final Answer:
It displays messages or values on the screen. -> Option AQuick Check:
print()shows output [OK]
Hint: Remember: print() means show on screen [OK]
Common Mistakes:
- Confusing print() with input()
- Thinking print() saves data
- Assuming print() creates variables
2. Which of the following is the correct way to print the words Hello and World separated by a space?
easy
Solution
Step 1: Understand how print separates items
Using commas inprint()separates items with spaces by default, soprint('Hello', 'World')prints "Hello World".Step 2: Check other options
print('Hello' 'World') joins strings without space; print('Hello' + 'World') concatenates without space; print('Hello'; 'World') uses invalid syntax.Final Answer:
print('Hello', 'World') -> Option CQuick Check:
Comma adds space in print() [OK]
Hint: Use commas to print multiple items with spaces [OK]
Common Mistakes:
- Forgetting commas between items
- Using semicolon inside print()
- Concatenating without spaces
3. What will be the output of this code?
print('A', 'B', 'C', sep='-')medium
Solution
Step 1: Understand the sep parameter in print()
Thesepparameter changes the separator between printed items. Here, it is set to '-'.Step 2: Apply sep to the items
Items 'A', 'B', 'C' will be joined with '-' between them, resulting in 'A-B-C'.Final Answer:
A-B-C -> Option BQuick Check:
sep='-' joins items with dash [OK]
Hint: sep changes space to your chosen separator [OK]
Common Mistakes:
- Ignoring sep and expecting spaces
- Confusing sep with end
- Missing quotes around separator
4. The code below gives an error. What is the problem?
print('Hello' end='!')medium
Solution
Step 1: Check syntax of print() arguments
Arguments inprint()must be separated by commas. Here, 'Hello' andend='!'are missing a comma.Step 2: Understand correct usage
Correct syntax isprint('Hello', end='!')with a comma separating the string and the keyword argument.Final Answer:
Missing comma between arguments -> Option AQuick Check:
Separate print args with commas [OK]
Hint: Separate print arguments with commas [OK]
Common Mistakes:
- Forgetting commas between arguments
- Thinking end is not allowed
- Misplacing quotes
5. How can you print the numbers 1, 2, and 3 on the same line separated by commas, but end the line with a period instead of a newline?
hard
Solution
Step 1: Use sep to set commas between numbers
Settingsep=','prints numbers separated by commas: '1,2,3'.Step 2: Use end to replace newline with a period
Settingend='.'prints a period instead of moving to a new line.Step 3: Check argument order
Argument order does not matter, but bothsepandendmust be included in the same print call.Final Answer:
print(1, 2, 3, sep=',', end='.') -> Option DQuick Check:
sep=',' and end='.' print 1,2,3. [OK]
Hint: Use sep for commas and end for period [OK]
Common Mistakes:
- Swapping sep and end values
- Using two print statements instead of one
- Forgetting to set end to '.'
