Lesson 2 of 60
โฎPrev Nextโฏ

PY-101 ยท Getting started with Python

print() and your first program

โฑ 9 min ๐Ÿ Real Python 3 ๐ŸŽฌ Animated

2of 60

Your note

My notes for this lesson

Select any text in the lesson to highlight it or add a note. Saved in this browser.

    No highlights yet. Select a sentence in the lesson, then pick a colour or add a note.

    All my bookmarks and notes

    Reading settings

    For every lesson, saved in this browser.

    Text size
    Line spacing
    Font

    With IaaS you rent the virtual machine. Microsoft looks after the building, power and hardware; you patch the operating system.

    โšก The 30-second answer

    print() is the built-in Python function that shows values on the screen. You can pass it several values separated by commas, and it puts a space between them and a new line at the end. The sep and end arguments change those two defaults.

    • Print text and numbers
    • Print several values at once
    • Control spacing with sep and end
    • Use \n for a new line

    Try it

    Watch it come alive, run it yourself, then prove it. Real Python, right on this page.

    Watch Python run the example. Every variable is a jar; watch the values change and the output appear.

    print() and your first program, running
    1. print("Hello, world!")
    2. print("Score:", 42)
    3. print("a", "b", "c", sep="-")
    4. print("No new line", end=" ")
    5. print("...continued")
    6. print("Line 1\nLine 2")
    Press โ–ถ to watch Python run this, one line at a time.
    1. The problemYour program runs, finishes, and shows nothing. Did it work? Without print() you cannot tell. Even senior developers still reach for print first when something looks wrong.
    2. The Python ideaprint(value1, value2, ..., sep=" ", end="\n")print() is the built-in Python function that shows values on the screen.
    3. In real lifeLog files are mostly print-style lines: a time, a level and a message joined by a separator. Changing sep is how you turn the same values into a CSV row.

    Start with a question

    Your program runs, finishes, and shows nothing. Did it work? Without print() you cannot tell. Even senior developers still reach for print first when something looks wrong.

    print() is how you see what your program is doing. Professional developers still use it every day to check values while they debug.

    Explain it like I'm 10

    print() is your program's voice. Whatever you put inside the brackets, it says out loud on the screen.

    The technical explanation

    Text must be inside quotes. Single quotes and double quotes work the same: print('hi') and print("hi") both print hi.

    When you pass several values, print("Score:", 42) prints them with one space between. Numbers do not need quotes.

    sep sets what goes between values and end sets what goes after the last one. By default they are a space and a new line.

    Inside a string, \n starts a new line and \t inserts a tab. These are called escape sequences.

    Syntax

    print(value1, value2, ..., sep=" ", end="\n")

    โ–ถ Run the example ๐ŸŽฌ Watch it come alive

    Real-world scenario

    Log files are mostly print-style lines: a time, a level and a message joined by a separator. Changing sep is how you turn the same values into a CSV row.

    Ready for more?The full lesson adds the technical explanation, interview questions and quick revision.

    Test yourself

    5 questions. Get 4 right and this lesson is marked complete.

    1. 1. What does print("a", "b") show?
    2. 2. What does print("x", "y", sep="") show?
    3. 3. Which argument stops print() from moving to a new line?
    4. 4. What does \n do inside a string?
    5. 5. Which line is valid Python?

    Questions people ask

    What is the difference between single and double quotes in Python?

    There is none for normal strings. Pick one style and stay with it. Use the other kind when your text contains a quote, for example "It's ready".

    Why does print need brackets in Python 3?

    In Python 3, print became a function, and functions are called with parentheses. The bracket-free form print "hi" only worked in Python 2, which reached end of life in 2020.

    Interview questions

    How this topic comes up in Python interviews. Answer out loud first, then open the model answer.

    beginner What is the difference between single and double quotes in Python?

    There is none for normal strings. Pick one style and stay with it. Use the other kind when your text contains a quote, for example "It's ready".

    beginner Why does print need brackets in Python 3?

    In Python 3, print became a function, and functions are called with parentheses. The bracket-free form print "hi" only worked in Python 2, which reached end of life in 2020.

    intermediate What does print("a", "b") show?

    a b. Values separated by commas are printed with a single space between them, because sep defaults to " ".

    intermediate What does print("x", "y", sep="") show?

    xy. sep="" puts nothing between the values.

    Quick revision

    Remember

    • Print text and numbers
    • Print several values at once
    • Control spacing with sep and end
    • Use \n for a new line
    • Commas add a space for you: print("Total:", 5) shows Total: 5. Do not put your own space inside the quotes as well, or you get two.

    Syntax

    print(value1, value2, ..., sep=" ", end="\n")

    Official documentation