Hello world in Python: Basic syntax to know


Every programmer started their journey in programming by first printing Hello World. So, we start by hello world in python. In line with this, we look into some of the basic elements or syntax you have to know before we directly drive into real python programming. It is a basic but very important element that you should be aware of and well familiar with.

Before we write our first Python program; hello world. You should know that python is case-sensitive. “Print” and “print” conveys different meaning in python. Capital letter “P” and small letter “p” is not the same though both the word holds the same meaning.

Let’s start by writing one line of code;

Hello world in python

Open any python text editor. If you haven’t install any text editor, read my post on how to install python IDLE.

Let’s write our first Python file, main.py.

Simple as that. Save your file as main.py and run.

The output will be Hello World! Congratulations, you have written and executed your first Python program.

The print() is a built-in function in python that produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters in it. We use this small letter print to print the text to the output window.

Quotations in Python

While printing hello world in python, you have seen that hello world is written inside the quotation mark enclosed by the bracket. Anything that is inside the quotation mark is a string and we called it a string literals. Python accepts single (‘), double (“) and triple (”’ or “””) quotes to denote the string literals.

Example (single and double quotation):

Example (triple quotes):

It is like python convention, that single quotes are used for words, double quotes for sentences, and triple quotes are mostly used to span the string across multiple lines.

There is one thing you should know regarding the use of quotations in python. Sometimes even though quotations are used correctly, it gives a syntax error. See the example below, code will run into error despite correct syntax.


hello world in python

How can we fix this error?

One is to escape the single quote by placing a backslash before it. The other is to use double quotes instead of single quotes as the enclosing quote. What I mean here is, sometimes quotation marks have to use appropriately whenever required.   Both ways are shown here.

Newline in Python

When we want to print the output in a new line or to break the statements inside the print function, we use “\n” before displaying the actual line.

Preview (opens in a new tab)

Multiline Statements

Every Python statement ends with a newline character. However, we can extend it over to multiple lines using the line continuation character (\) to denote that line should continue.

Python gives us two ways to enable multi-line statements in a program. 

Explicit line continuation: When you right away use the line continuation character (\) to split a statement into multiple lines.

Implicit line continuation: Implicit line continuation is when you split a statement using either parentheses ( ), brackets [ ] and braces { }.


Note: In the above example, math, dzo, eng, result, and total are called variables. A Variable is used to store some data. When you are printing the content of the variable, the variable name should not be inside the quotation mark. Otherwise, it will interpret as string literals, not as a variable.

Line Indentation

Python’s unique feature from other programming languages is that it takes care of line indentation. Line indentation is used to indicate blocks of code for class, functions, and control flow statements. The line indentations in python help in making the code clean, manageable, and more readable. Other programming languages use semicolons or parentheses to identify the block of code.

By default, when we indent the block of code, it takes four white spaces. However, you can change by configuring the IDE. You can change it to 1 or more white spaces to represent the block of code.  You will understand the essence of line indentation more when you deal with control flow statements, functions, and classes. This is the beauty of python. No need to worry about your code getting unstructured. Line indentation helps in structuring the code.

In the example below, no need to worry about how for loop works or what is range function. You just need to understand that python takes care of line indentation.  

Wrong block of code:

Indentation error

The correct block of code:

One important basic thing to know is how to write comments while coding and its importance. Read on how to write comments in python.