Displaying variable data in Python Video

Displaying variable data in Python

Jeremiah Taguta (BSc in Computer Science, Masters in Information Systems)
Published on 18-03-2023
Printing variables of different data types with labels.

Need for output

In our previous blog at Introduction to Python programming, we created variables of diverse data or object types. Variables may be declared to store the results of some processes and their values need to be displayed for users to see. Original data stored in variables may need to be output to the user. It becomes important that we discuss how Python provides output or print variables with or without strings. Below are the variables from the previous article on the link provided above,

1 """
2 variable declaration of different object/data types. A multiline comment
3 """
4 name = "James" # string
5 age = 34
6 height = 1.65
7 complete = True
8 finished = False
9 message = "Hello World"
10 num_list =[1, 2, 3]
11 num_tuple =(1, 2, 3)
12 num_set ={1, 2, 3}
13 person = {'name': 'Peter', 'age': 43, "job": "developer"}


Just to explain something, to make your code understandable, it's a good practice to comment on it. After so many months, you should not struggle to remember the purpose of your code as comments become useful. That way your code becomes easy to maintain. Comments are ignored by the Python interpreter during the execution of your program.

For a single-line comment, use a # as in line 4 above.

For multiline comments, use triple single or double opening and close quotes as shown on lines 1-3 above.

Printing variables

For our console-based programs, we need not reinvent the wheel, Python has a built-in function for output i.e. print(). The output will be displayed on the console. Each print function call uses a new line. This need not be imported, just call it.

The print function formats any data or arguments that are given into a stream. It can be called with no arguments as in line 1 below and thus prints a new line.

NB: To execute your code, remember to run your code.

1 print()

To print a variable, no matter the type of data stored therein, call the print function with the variable name as an argument as below. By the way, an argument is what is put in the parenthesis when a function is called. Arguments can be nothing, 1 or more (separated by a comma). The print function can handle any number of arguments.

1 print(age)

The print function

When you seek help on the print function from the help function like in line 1 below, you will see

Help on built-in function print in module builtins:


    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)

    Prints the values to a stream, or to sys.stdout by default.

    Optional keyword arguments:

    file:  a file-like object (stream); defaults to the current sys.stdout.

    sep:   string inserted between values, default a space.

    end:   string appended after the last value, default a newline.

    flush: whether to forcibly flush the stream.


This means the print function takes a value or many values for printing. It takes a sep, end and file argument which we shall talk about later

1 print(help(print))

Below is how we print a string on its own, a point to note is that a string value should always be in quotes. So should the variable storing the string be in quotes? No, but when it's declared. All variables are printed like we did on line 1 above.

1 print(message) # message variable need no quotes though storing string
2 print("Hello, my world") # quotes needed

Printing variables with labels

Since the value of 34 is stored in the variable, age, you will just see a 34. After a year of writing the program, you won't know what 34 represents unless you read the code. What more is the user with no programming knowledge? 

This makes it essential to label our output using a string as shown below.

1 print("Your age is :", age)

Formatting - GIGO

A string value "Your age is" is printed as a label to the variable message. Note how different arguments, are separated by a comma. 

The print function can handle many arguments, just separate them by a comma, and print will join them into one string with space between arguments. 

Programming follows the GIGO principle, i.e., garbage in, garbage out. Don't expect the Python interpreter to put a space between "is" and ":", if you need it, put it as in line 1 above. Same with "," on line 1 below to make our results meaningful. " " or space is an empty string that can be printed too as shown on line 3 below.

Below is a snippet printing our variables with a label, and each print function call starts on a new line.

1 print(name, " is ", age, "years old. He has a height of", height,", finished programming ? ", finished,", adult ?",complete )
2 print() # empty line
3 print("numbers list :", num_list, "numbers tuple :", num_tuple , "Numbers set :", num_set)
4 print("   after 3 spaces") # string of 3 empty spaces
5 print("Personal Info :", person)
Thanks for reading. If you have any questions, feel free to ask in the comments section, I'll gladly help. Check out my programming tutorials on YouTube and don't forget to subscribe, comment, like, and share on both YouTube and on this Blog. Lets code together!!!

162 Like

Drop your comment

© Copyright 2023 Jeremiah Taguta. All Rights Reserved.

Product of ‌

Jeremiah Taguta T/A Charis Technologies