Displaying non-variable data in Python Video

Displaying non-variable data in Python

Jeremiah Taguta (BSc in Computer Science, Masters in Information Systems)
Published on 26-03-2023
Printing data of diverse data types that are not stored in variables and using the sep and end print function arguments.


The Rationale

There are times when we need to output data. In the last article, Displaying variable data in Python, we covered printing variables of many data types with labels. But it's not every data that must be stored in a variable as it may not be used anywhere else in the program. Such data has to be printed on the fly.

In the last article with the link provided above, we printed the undeclared string value "Hello, World". We also printed variables along labels, the labels were strings.

To print many values, we still pass the values as arguments in a print function separated by commas. 

Strings

This rule of separating values by commas does not apply to string values as you can see on line 2 below. 

1 print("Hello World!") # string value
2 print("Hello " "World") # no need for , between string values

Output  values of different types with labels

Our labels are always strings. The values on the right of the comma are written in the manner they would have been initialized in a variable. 

String values are always written in quotes. All we are doing is removing the variable name but leaving the value in the original format we declared it in our 2 previous articles. Why? because one way to distinguish a list from any other data type is to use [], () for tuples, {} for sets, {key: value} for dictionaries, "" for strings and other many constructs for other data types. Refer to Introduction to Python programming for more.

I reiterate that to print values on the fly, they must be typed in the same way they would have been assigned to a variable. That's what makes them different data or object types. For any type of data, there are rules to be followed to use it anywhere in a Python program, either on the fly or storing it in a variable.

See the example below.

1 print("name :", "James") # string
2 print("Age", 34)
3 print("height", 1.65)
4 print("completed :", True)
5 print("finished :", False)
6 print("message :","Hello World")
7 print("numbers list",[1, 2, 3])
8 print("numbers tuple :", (1, 2, 3))
9 print("numbers set :",{1, 2, 3})
10 print("Person info :", {'name': 'Peter', 'age': 43, "job":"developer"})

Cause different functions to use a single line: end argument

As I highlighted before, every print starts on a new line. By default, end ="\n" for the print argument. This means any print ends with a new line. This behaviour can be altered though to force many print functions to use a single line using the end argument. 

The end argument has to be assigned a string you want to use to mark the end of the print function on the same line. E.g. line 1 below uses a " $ " while line 2 use " ?* " and line 4 use " ". We had to put space before $ to make the output readable. It can be assigned any string and pulls the next print function from the next line to its line. 

Line 1 below causes line 2 print to come to line 1 and so does line 2 to line 3. But line 3 has no end argument so line 4 is by default on its line but pulls line 5 onto its line. Line 5 has no end argument so line 6 starts on its own line.

1 print("Using the end #" "name :", "James", end=" $ ") # string
2 print("Age", 34, end="?*")
3 print("height", 1.65)
4 print("completed :", True, end = " ")
5 print("finished :", False)
6 print("message :","Hello World")

Customize Separator values printed by single function

To make it easier to learn, I incrementally add more flesh as we go. So far, printing many values using one print separates the values on output by one space. By default, the Python print function has sep= " " as its argument. 

The sep argument can thus be customized with a string value of your choice. Line 1  below use default sep = " " and line 2 use sep = " || ". Thus, customization of the print function separator. 

1 print("With no sep #"  " numbers list",[1, 2, 3], "numbers tuple:", (1, 2, 3),"numbers set :",{1, 2, 3})
2 print("With sep #"  " numbers list",[1, 2, 3], "numbers tuple :", (1, 2, 3),"numbers set :",{1, 2, 3}, sep= " || ")
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!!!

187 Like

Drop your comment

© Copyright 2023 Jeremiah Taguta. All Rights Reserved.

Product of ‌

Jeremiah Taguta T/A Charis Technologies