Printing Output in Python

Introduction

Welcome to Writing and Running Your First Python Programs! This is the very first lesson of your very first unit, so we are standing right at the starting line together. Whether you have never written a single line of code before or you are just brushing up on the basics, you are in exactly the right place.

By the end of this lesson, we will understand what a programming language actually is, we will have written a small program that greets the world and shows a number, and we will know how to run that program ourselves. Let's begin from the very beginning.

What Is a Programming Language?

A computer is a machine that follows instructions extremely well and extremely fast. There is a catch, though: computers do not understand human languages like English. They need their instructions written in a special, precise form called a programming language.

A programming language is a set of words and rules that lets us tell a computer exactly what to do, one step at a time. There are many such languages, and the one we are learning is called Python. Python is famous for being clear and beginner-friendly, because it often reads almost like plain English.

A few words we will use constantly:

  • The instructions we write are called code.
  • A file containing code can be a program, especially for small scripts like the ones we will write here. Larger programs are often built from many files working together.
  • When we ask the computer to follow those instructions, we say we run the program.

How Does Code Actually Run?

Deep down, a computer only understands a stream of very simple signals. We would never want to write instructions at that level by hand. This is where a helper program called the interpreter comes in.

When we run a Python program, the interpreter reads our code from top to bottom, one line at a time, translating each instruction and carrying it out. We simply write clear Python code, and the interpreter handles all the messy translation for us.

That single idea, top to bottom, one line at a time, is worth remembering. For the simple straight-line scripts in this lesson, Python runs our statements in the exact order we write them. Later on, we will meet features that can let a program skip around or repeat steps, but for now, top to bottom is all we need.

Making Your Program Talk

On its own, a computer keeps everything hidden inside itself. It can happily crunch numbers and shuffle data around, yet nothing appears on our screen unless we explicitly ask for it.

That is the job of print(). It is a built-in function, which means Python already knows what it does, so we do not have to define it ourselves. When we call print(...), Python takes whatever we place inside the parentheses and displays it in the output area.

Two small pieces of vocabulary before we continue:

  • A string is a piece of text wrapped in quotation marks, such as "Hello".
  • A number is written plainly, without any quotes, such as 42.

We will use both in the examples ahead.

Printing Text with print()

Let's start with the classic first program of nearly every programmer: printing a friendly greeting.

print("Hello, world!")

Let's break this down carefully:

  • print is the name of the function we are calling.
  • The parentheses ( and ) tell Python to run this function using whatever is inside.
  • The quotation marks around Hello, world! tell Python that this is a string of text, not code to execute.

When this line runs, Python displays the text and then moves down to a new line, so anything that comes next starts fresh below.

Hello, world!

Notice how the quotation marks themselves are not shown in the output; they are just a signal to Python that we mean text.

Printing Numbers with print()

Text is not the only thing we can print. We can hand print() a number just as easily, and this time we do not use any quotation marks:

print(42)

Here, 42 is a raw number, so Python treats it as a numeric value rather than a sequence of characters. The result is displayed on its own line:

42

A helpful mental rule for now: text strings are written inside quotes, while numeric literals such as 42 are written without quotes. Mixing these up is one of the most common early mistakes. Writing print("42") would still show 42, but Python would treat it as text rather than a number, which matters a lot once we start doing math.

Printing Multiple Values in One Call

What if we want to combine text and a number in the same line of output? We do not need two separate calls; we can pass several values into one print(), separated by commas:

print("The answer is", 42)

We are giving print() two values at once: the string "The answer is" and the number 42. Python prints them in order, and, as a nice bonus, it automatically inserts a single space between each value, so we do not have to add one ourselves.

The answer is 42

This comma trick works for as many values as we like, and it is a quick way to build simple messages that mix words and numbers.

Running Your Code with python main.py

Now let's put everything into one complete program. Our code lives in a file, and by convention we will call ours main.py. The .py ending marks it as a Python file.

print("Hello, world!")
print(42)
print("The answer is", 42)

To actually run this file, we use a terminal (also called a command line), which is a place where we type commands for the computer. We type:

python main.py

This tells the Python interpreter, "open the file main.py and run it." The interpreter then executes the file from top to bottom, and each print() call produces one line of output, in the same order the calls appear:

Hello, world!
42
The answer is 42

Three print() calls produce three lines of output. If something looks off, no worries: we can fix the code and run python main.py again as many times as we like. Re-running is free, and it is one of the best ways to learn.

A Quick Note on Typos

Because Python reads our code so literally, a small typo will stop the program before it prints anything. The most common slips at this stage are:

  • A missing closing quotation mark: print("Hello, world!)
  • A missing closing parenthesis: print("Hello, world!"
  • Curly "smart" quotes copied from a document: print(“Hello, world!”) instead of straight " marks

In each case, Python reports a syntax error — its way of saying, "I could not make sense of this line." For example, running the file with the missing closing quote prints something like this instead of our greeting:

  File "main.py", line 1
    print("Hello, world!)
          ^
SyntaxError: unterminated string literal (detected at line 1)

The message usually names the file and the line number, which is exactly where to look first. Check that every opening quote has a matching closing quote and every ( has a matching ), fix the line, and run the program again.

Conclusion and Next Steps

Nicely done! In this first lesson, we learned that a programming language lets us give a computer precise instructions, that Python code runs from top to bottom one line at a time, and that we run a program with the command python main.py. We also met our very first function, print(), and used it to display text, numbers, and several values in a single call.

That is a solid foundation for one lesson, and the best way to lock it in is by writing code yourself. Up next is a short set of hands-on practice tasks where you will craft your own print() calls and watch your programs speak back for the first time. Let's go!

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal