Remember
print() converts its arguments to text and writes them to output. sep separates arguments; end controls what follows them.
print() converts its arguments to text and writes them to output. sep separates arguments; end controls what follows them.
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
word = 'tea'
print(word, "ready", sep=" | ", end="!\n")Top-level scope
'tea'print() converts its arguments to text and writes them to output. sep separates arguments; end controls what follows them.
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
word = 'tea'
print(word, "ready", sep=" | ", end="!\n")
|
v
DISPLAYED RESULT
tea | ready!
FINAL BINDINGS
word --> object 1 (str)
object 1: 'tea'Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
word = 'tea'
print(word, "ready", sep=" | ", end="!\n")Top-level scope
'tea'The example has finished. Compare the final output with the values and references above.
tea | ready!
What do you expect this program to print?
word = 'tea'
print(word, "ready", sep=" | ", end="!\n")print() FunctionThe print() function is one of the most commonly used built-in functions in Python. It displays the given message or values on the screen (standard output).
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
*objects – One or more expressions to be printed.sep – String inserted between values. Default is a space.end – String added after the last value. Default is newline '\n'.file – Where to print. Default is the console (standard output).flush – Whether to forcibly flush the stream. Default is False.print("Hello, World!")
Hello, World!
print("Name:", "Alice", "Age:", 25)
Name: Alice Age: 25
print("apple", "banana", "cherry", sep=", ", end=".")
apple, banana, cherry.
for i in range(3):
print("Line", i)
Line 0
Line 1
Line 2
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
My name is Alice and I am 25 years old.
with open("output.txt", "w") as f:
print("Writing to file", file=f)
print without parentheses in Python 3 (works in Python 2 only)print() to return a value – it returns Noneprint() is often used for debugging. Knowing how to format outputs and print in loops can help during problem solving rounds.
print() sends output to the screen.sep, end, and file.Write a Python program that asks for your name and prints: Hello, <your name>!
name = input("Enter your name: ")
print("Hello,", name + "!")
Connect what you noticed in the example with the idea below.
print() converts its arguments to text and writes them to output. sep separates arguments; end controls what follows them.
sep controls the text that follows the last argument.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.