[vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Prints in Python – Hello World!

[/vc_column_text][vc_separator type=”normal” up=”1″][vc_column_text css=”.vc_custom_1570445221050{background-color: #f6f6f6 !important;}”]

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

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

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 encode yourself, directly in the Jupyter Notebook.

 

 

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

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

HOW TO MAKE A PRINT 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!

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text][/vc_column_text][vc_empty_space height=”20px”][vc_column_text]Note that you put the text you want to print quotation marks ( ” ” ). To run your program, click Run in Jupyter Notebook.[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

FORMAT YOUR PRINTING 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.[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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.[/vc_column_text][vc_empty_space height=”10px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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!

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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!

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”10px”][vc_separator type=”normal” thickness=”2″][vc_empty_space height=”10px”][vc_column_text]

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

[/vc_column_text][vc_empty_space height=”20px”][vc_column_text]

[/vc_column_text][vc_empty_space height=”20px”][vc_separator type=”normal”][vc_empty_space height=”20px”][/vc_column][/vc_row][vc_row css_animation=”” row_type=”row” use_row_as_full_screen_section=”no” type=”full_width” angled_section=”no” text_align=”left” background_image_as_pattern=”without_pattern”][vc_column][vc_column_text]

Previous Page    |    Next Page

[/vc_column_text][vc_empty_space][/vc_column][/vc_row]