The print() function shows messages or results on the screen. It helps you see what your program is doing.
print() function basics in 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).
print('Hello, world!')
print('Hello', 'there')
print('Hello', 'there', sep='-')
print('Hello', end='!') print(' How are you?')
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.
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!')
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.
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
print() function do in Python?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]
- Confusing print() with input()
- Thinking print() saves data
- Assuming print() creates variables
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]
- Forgetting commas between items
- Using semicolon inside print()
- Concatenating without spaces
print('A', 'B', 'C', sep='-')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]
- Ignoring sep and expecting spaces
- Confusing sep with end
- Missing quotes around separator
print('Hello' end='!')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]
- Forgetting commas between arguments
- Thinking end is not allowed
- Misplacing quotes
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]
- Swapping sep and end values
- Using two print statements instead of one
- Forgetting to set end to '.'
Ready to prove it?
Pass the test and this pattern moves unknown
