Practice: Print Function
Q1. Print three words
Print the words red, green and blue on one line separated by commas and a space, using sep.
Goal: Use sep.
Q2. Stay on one line
Print the numbers 1 to 5 on one line separated by spaces using a loop and end.
Goal: Use end.
Q3. Date format
Print the day 15, month 03 and year 2025 as 15/03/2025 using sep.
Goal: Use sep with mixed types.
Q4. Two lines in one call
Print Hello and World on two lines using a single print call with a newline escape.
Goal: Use \n.
Q5. Tab table
Print a header Name and Score separated by a tab, then Asha and 90 separated by a tab.
Goal: Use \t.
Q6. Box line
Print a line of 12 stars, then the text Done, then another line of 12 stars.
Goal: Repeat strings.
Q7. Unpack a list
Print the items of [4, 8, 15] on one line with ' | ' between them without using a loop.
Goal: Use * unpacking.
Q8. Debug display
Given a = 3 and b = 4, print both names and values in the form a=3 then b=4 using f-string equals.
Goal: Use {x=}.
Q9. Default separator
What does this print?
print("a", "b", "c")Goal: Know default sep.
Q10. End argument
What does this print?
print("A", end="-")
print("B", end="-")
print("C")Goal: Predict end.
Q11. Return value
What does this print?
r = print("hi")
print(r)Goal: Know print returns None.
Q12. Escape lengths
What does this print?
s = "a\tb\n"
print(len(s))Goal: Count escape characters.
Q13. Raw string
What does this print?
print(r"x\ny")
print("x\ny")Goal: Compare raw and normal strings.
Q14. Sep with newline
What does this print?
print(1, 2, 3, sep="\n")Goal: Use newline as sep.
Q15. Quotes in lists
What does this print?
print("hi")
print(["hi"])
print(("hi", 1))Goal: Know str versus repr display.
Q16. Fix the Python 2 print
This old-style line fails in Python 3. Fix it so it prints Welcome.
print "Welcome"Goal: Fix print syntax.
Q17. Fix the separator
The date should print as 2025-01-05 but prints with extra spaces. Fix it.
print(2025, "-", "01", "-", "05")Goal: Fix a separator mistake.
Q18. Fix the path
The path should print as C:\temp\new but the escapes spoil it. Fix it.
print("C:\temp\new")Goal: Use raw strings.
Q19. Why None
Why does text = print("hello") leave text equal to None?
Goal: Explain print's return value.
Q20. stdout versus stderr
Why might a program print error messages with file=sys.stderr instead of the normal print?
Goal: Explain streams.