How to print statements and variables in Python

How to print statements and variables in Python

Introduction

Python programming language is a powerful language used to build amazing web and mobile applications. This article teaches you how to print statements and variables in Python. If you are a beginner at Python programming, then this article is for you.

At the end of this article, you should be able to know :

  • What a variable is
  • What a statement is in programming
  • How to print out variables and statements in Python

Prerequisites

  • Have a basic knowledge of what a variable is

  • Install Anaconda Prompt on your laptop

What are variables ?

(You could pass this if you already have an idea about the meaning of variables in programming)

A variable in Python or any other programming language is a reserved memory location in which information can be stored in order to be used later by a computer program.

On this blog, you can learn more about variables in Python.

What is Anaconda ?

Anaconda is a powerful tool used to perform some tasks in Python through a command line interface. Here is a step-by-step guide to install Anaconda distribution on your laptop.

Whether you are a Linux or a Windows user, to ensure that Anaconda is successfully installed on your machine, you can search it on your laptop and execute it. If you are a Windows user, you should have this screen :

image.png Then you can click on Open.

What does the print() function do ?

Among the different built-in functions provided by Python, there is the print() function. This function simply helps the programmer to display a content on the screen. This content can be a simple string, a string located in a variable, a number or whatever.

Here is the basic syntax to print out a statement :

print (YourStatement)

Next, you are going to see the different ways to print something using anaconda prompt.

Step 1 : Open your Anaconda

On your laptop, search Anaconda prompt and open it. Below is the screenshot of the screen you will get. The only difference is that you will have your username instead of modoukpe.

image.png

In the Anaconda command line interface, type python and click on Enter. You will notice that the prompt appear, just like on the following image.

image.png

The presence of the prompt (>>>) simply means that the Python interpreter is ready to execute python code block.

Step 2 : Print a statement / string / variable

In this part, you are going to work on an example. Previously, you have learnt the syntax used to print something in Python. Now, you will practice it. In your anaconda prompt, type :

print ("You are my rainbow")

Next, click on Enter. But before, make sure that the syntax you type is correct. Then, you will surely have it displayed like this on your screen :

image.png

There is an interesting fact with the print statement in Python. Whether it's a double quotes, single quotes or even triple quotes, the result will remain the same.

To be sure, you can merely put it in practice. The following picture shows it.

image.png

If you want to print a string or a statement, keep in mind that the quotes are very important, so you should always put them.

Printing a variable

The syntax is the same with printing a statement. The only difference resides in the fact that you have to first declare and assign a value to your variable. For example, you can try this :

myVariable = "I am turning 22."
print (myVariable)

As you can notice, there are no quotes around the myVariable because it's a variable and not a statement. On the next image, you can see the output of these instructions.

image.png

Other few ways of printing

Let's say that you want to display Good Morning on your screen, but the second part of that statement, Morning is inside a variable you will name moment.

That simply means that you will first create the variable moment, assign to it the value Morning and finally concatenate it with Good.

Example 1

This first example illustrates the common way used to concatenate strings in Python. You just need to separate the different strings or variables with a comma (,). Like this :

moment = "Morning"
print ("Good",moment)

Here is the output :

image.png

Example 2

The second way you are going to learn in this article is to use the plus (+) sign to concatenate two or more elements.

moment = "Morning"
print ("Good " + moment)

Output :

image.png

Please, note that if you do not leave an additional space after the word "Good", your result will be : GoodMorning, like on the next screenshot :

image.png

To fix it, you have two choices :
1- You can either add an additional space after the word "Good", as you learnt on the first example.

2- Or you can add a space before the second word, like on the picture below :

image.png

Example 3

moment = "Morning"
print ("Good {}".format(moment))

Output :

image.png

Example 4

moment = "Morning"
print (f"Good {moment}")

Output

image.png

Example 5

Here's another way to print concatenated variables. The %s refers to strings.

moment = "Morning"
print ("Good %s" % moment)

image.png

What about other datatypes ?

About the last example, you learnt the use of %s but this is only applicable for strings. Here are a few examples of symbols to use according to the type of the data you want to print.

  • %d for Integer
  • %e for exponential
  • %f for Float
  • %o for Octal
  • %x for Hexadecimal

If you succeed applying these steps, congratulations.

image.png

Conclusion

This article is a simple guide on how to print statements and variables in Python. There are some basics a beginner in Python programming should know before, like the definition and use of variables. Mainly, these points have been covered :

  • Variable definition
  • Quick guide to Anaconda installation
  • Statements/strings and variables printing
  • Strings and variables concatenation

Want to make any comment or suggestion ? Feel free to do so !

Hope this makes you learn something new in Python.

Stay safe !

Screenshots & Cover Photo : By me