Hello World! Your first program in Python


Keywords: Hello World, Python

Hello World Learn Python

In Python, you display a result in the console using the print () function. In our first program, we will create a program that shows the text line “Hello World!”.

Prints in Python

Now it’s time to code your first program! It is an old tradition that a programmer’s first program prints “Hello World!” If you want, you can code it yourself, directly in the Jupyter Notebook.

It is an old tradition that a programmer’s first program prints “Hello World!”

How to display text in the console in Python?

Printing in Python is easy. You simply use the print () function

For example, like this:

print("Hello World!")

When you run the program, you will then receive the printout:

Hello World!

Note that you put the text you want to print quotation marks ( ” ” ). To run your program, click Run in Jupyter Notebook.

Formatting your text in Python

There are several ways to format your printing in Python, in other words, how you can format your prints to make them look the way you want them to. Below we will se some examples how to easily adjust our prints.

Multiple prints at the same time

For example,

a = "Programming in Python" 
b = " is fun" 

print (a, b)

Gives the print:

Programming in Python is fun

Prints on multiple rows

You simply add \ n to make a line break

print ("Programming in Python", "\nis fun")

Gives the print:

Programming in Python 
is fun

Note,  there is no space after \n, if we would have put it then the print becomes:

Programming in Python
 is fun

In other words, an extra space after the line break.

Prints with a Tab

You simply add \ t to make a tab

a = "Programming in Python"
b = "is"
c = "Fun!"

print("\na:\t", a, "\nb:\t", b, "\nc:\t", c)

Gives the print:

a:    Programming in Python 
b:    is 
c:    Fun!

Create Placeholders

You use the brackets to create so called Placeholders. For example,

a = "Programming in Python is {}"

print(a)

With print:

Programming in Python is {}

Furthermore, by using the command format ( ) we can use the placeholder to add values

b = a.format('FUN!')
print(b)

With the new print:

Programming in Python is FUN!

Be careful when using mathematical operations

The text you want to print should be inside the quotation marks (” “). Be careful not to forget these, in that case, there is the risk that you will perform a mathematical operation instead.

Let’s take a example to show what we mean,

print(1+1)

Gives print:

2

And the code:

print("1 + 1")

Gives the print

1 + 1

Avoid incorrect syntax

If you would make a mistake and forget the , for example, quotation marks, you will get a so-called compilation error. This means that the compiler does not understand your code and the program will then crash. If we try to print “Hello World!” Without the quotation marks, we will receive an error message.

print(Hello World!)

Resulting in:

SyntaxError: invalid syntax

Summary: Display texts in Python

Now we have learned how to display text in different ways in Python using the print () function. So if we summarize:

  • The print () function display a text in Python
  • Everything you enter in parentheses of the print() function will be displayed in the terminal window
  • What you want to print is indicated with quotation marks “”
  • You can print values of variables by entering the variable in the function
  • A mathematical operation is printed directly in the function. Be careful!
  • You create a new row with \n
  • An indent is created with \t
  • Format a text string and create a placeholder with seagull wings {}