Python 3D Academyবাংলা

Printing with print()

🌍 In real life. At a railway station, you hand a note to the announcer and they read it out over the loudspeaker, word for word. They don't fix your spelling or change the order; they just announce what you gave them.

In Python, print() is that announcer. Whatever you put inside its brackets is shown in the output, the area under the editor where your program talks to you.

The idea in one sentence: print() shows what is inside its brackets in the output, then moves to a new line.

Its words:

  • function: a ready-made command that does one job; print is one
  • calling a function: writing its name with brackets, print(...), to make it do its job
  • string: text inside quotes, "like this" or 'like this'

Step by step, for print("Hello"):

  1. Python reads the name print and sees the opening bracket.
  2. It picks up what's inside: the string "Hello".
  3. It shows the text, without the quotes, and starts a new line.

How Python reads what's inside. The quotes are the key:

  • In quotes, it's text. Python shows it exactly as written, even "2 + 3".
  • Without quotes, Python works it out first. 2 + 3 is maths, so it shows 5. A plain word like Hello would be a name to look up, and you haven't made any names yet.

Double quotes and single quotes do the same job; just use the same kind on both sides.

More than one thing at once. Separate the items with commas, and print puts one space between them. And an empty print() shows an empty line.

print("Hello")
print('Single quotes work too')
Double quotes and single quotes both work. Just use the same kind on both sides.
print(7)
print(2 + 3)
print("2 + 3")
See the difference? 2 + 3 is maths, so Python prints 5. But "2 + 3" in quotes is just text.
print("Apples:", 5)
print("I am", 20, "years old")
print("Line one")
print()
print("Line three")

Mistakes and edge cases.

  • Forgetting the brackets. print "Hi" gives SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)? (very old Python allowed it; today's doesn't).
  • Mixing quote kinds. print("Hi') gives SyntaxError: unterminated string literal: the string started with " and Python is still looking for the closing ".
  • Text without quotes. print(Hello) gives NameError: name 'Hello' is not defined.
  • A double space by accident. print("Apples: ", 5) shows Apples: 5 with two spaces: one from inside the quotes and one from the comma.
  • Maths in quotes isn't maths. print("2 + 3") shows 2 + 3, not 5.

Quiz

  1. What does this print?

    print(4 + 4)
  2. And this one?

    print("4 + 4")
  3. Which line is correct Python?

    • print("Hello")
    • print(Hello")
    • Print("Hello")
  4. What does this print?

    print("Score:", 10)
  5. Which line has a mistake?

    print("One")
    print("Two)
    print("Three")

Coding tasks

  1. Show exactly this:

    Hello, World!

  2. Use one print() with a comma to show:

    Apples: 5

    Write the number 5 without quotes.

  3. Show this, with an empty line in the middle:

    Top
    
    Bottom