Oh... 🙂, you're here. I guess you're just learning to code (welcome to the alternate universe) or you already learned to code but skipped a lot of the boring part. Well, you're here, so how about i take you far back, as early as print()
.
In this article I'll be taking you through Python's inbuilt function print()
, and also a bunch of stuffs that'll be useful as you continue your journey in this universe.
What you'll learn
•Hello world in python
•Printing multiple statements
•The print's sep keyword
•The print's end keyword
•Packing and Unpacking
•Supressing the print function
Hello, world.
if you started your programming journey from tutorials, or an article like this, Hello world
, must have been your first program (it wasn't for me...).
A "Hello, World!" program generally is a computer program that outputs or displays the message "Hello, World". In python, it'll be written like this:
print("Hello, world")
We wouldn't really be focusing on the basics, so let's get down to business.
Printing Multiple Objects
The print function is used to print python objects to the console, there might be cases where we would need to print multiple objects to the screen. all we need to do is add multiple arguments to the print
function, and we'll get a clean result.
is_active = True
name = "Harry Potter"
language = "JavaScript"
print("rubbie knows", language)
print(name, "is active:", is_active)
print("hello, world.", "my name is Rubbie")
# Unpopular opinion:
print("hello, world."+" my name is Rubbie")
someone from the crowd: "what's the difference between
print('hello, world.', 'my name is Rubbie')
andprint('hello, world.' + ' my name is Rubbie')
?..."
okay... looks like i just got a question 😂...
In the first example (print('hello, world.', 'my name is Rubbie')
) we're actually printing two string arguments, which is automatically separated by a whitespace once printed.
In the second example, we're printing a single string argument, both strings are just added together in the print
function's parentheses. this produces the same results, but may not work so well on other datatype asides string.
# you can't add a string and an integer
print("i am"+6) # --> TypeError
# both lines produces different results
print(5, 5) # --> 5 5
print(5+5) # --> 10
Separating arguments with the sep
keyword argument
Ok so... let me tell you a story
Rubbie woke up in the morning.
He took his dog down the street for a walk.
Few minutes later, he came back home.
the end.
if we were to write this story using the print
statement, we'll most likely write it using multiple print statements to preserve newlines.
print("Rubbie woke up in the morning.")
print("He took his dog down the street for a walk.")
print("Few minutes later, he came back home.")
print("the end")
If we were to write this same story with one print
statement and still preserve the new lines we'll pass "\n"
to the sep
keyword argument .
someone: how do we go about this?
rubbie: we'll pass each line of the story as different argument, and the set the sep keyword argument to a new line character. the sep keyword argument is set to a whitespace character by default
print(
"Rubbie woke up in the morning.",
"He took his dog down the street for a walk.",
"Few minutes later, he came back home.",
"the end.",
sep="\n"
)
To break this down for you, here's what the print function does with sep
the background; it joins all the arrangements in the function, and puts the value of sep
in between, giving this result:
Rubbie woke up in the morning.
He took his dog down the street for a walk.
Few minutes later, he came back home.
the end.
Other examples:
print("hello", "world", sep=",")
# result: hello,world
print(3, 5, sep="*")
# result: 3*5
The print
function's end
keyword.
you must have noticed, when we use the print statement to print a text to the screen, it ends with a newline, making the next text to be written below it.
print("hello")
print("world")
Outputs:
hello
world
To make the next printed statement start next to the previous line, we can pass a value to the end
keyword argument.
print("hello", end=" ")
print("world")
# result: hello world
the end
keyword argument simply adds an object to a printed line, and defaults to "\n"
Packing and unpacking
This part isn't only common to the print function, arguments and keywords arguments can be packed in to other functions too, but this is something i thought i shouldn't leave out.
Let's say we have a list that contains some data.... and we wanted to price them individually, let's say list of days. to print them out, we may have to use a loop:
days = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
for day in days:
print(day, end=", ")
print("\n")
we should have the following output after running the code above
Mon, Tue, Wed, Thur, Fri, Sat, Sun,
We got what we asked for, but took some really crooked path getting it. By unpacking the list into arguments for the print function, we can get a shorter, more efficient way of getting this done.
days = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
print(*days, sep=", ")
Notice the *
placed before the days
list inside the print function... it passes each item of the list as an argument to print
.
Here's are some practical examples.
name = "rubbie kelvin"
languages = ["python", "javascript", "c++"]
print(name, "can write in", end=" ")
print(*languages, sep=", ")
def inclinedplane(char, height):
char = char[0] # we need only one character
return [char*i for i in range(1, height+1)]
result = inclinedplane("*", 10)
print(*result, sep="\n")
Note: if you don't understand line2 of the code above please read on list comprehension in python
Suppressing the print function.
The print function is a good debugging tool, you can print out messages at different parts of your code and see what part the code makes an error. but for the times we wont be debugging, we may want to remove all our print statements, which can be very stressful if writing a large script.
to disable the print function, we'll overwrite the standard output.
import sys, os
def disable_print():
sys.stdout = open(os.devnull, 'w')
def enable_print():
sys.stdout = sys.__stdout__
print("this will be printed on console")
disable_print()
print("this wouldn't be printed")
enable_print()
print("re: this will be printed on console")
There should be a couple more print tricks, but I'll stop right here. I guess you just discovered some parts you missed out while reading this article 🙂.... well, glad i could help. please give feedbacks and ♥️ this article.