rubbiekelvin@localhost ~ $
X/TwttrGithubMailLinkedIn

As early as print()

June 25, 2020, 12:43 PM

·

python

programming

revised-post

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 parts. 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 stuff 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
  • Suppressing 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') and print('hello, world.' + ' my name is Rubbie')?"

Okay... looks like I just got a question 😂. In the first example, we're actually printing two string arguments, which are 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 data types aside from strings.

# you can't add a string and an integer
print("i am" + 6)  # --> TypeError

# both lines produce different results
print(5, 5)  # --> 5 5
print(5 + 5)  # --> 10

Separating Arguments with the sep Keyword Argument

If we were to write a story using multiple print statements, we'd want 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 wanted to write this same story with one print statement and still preserve the new lines, we'd pass "\n" to the sep keyword argument.

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"
)

Other examples:

print("hello", "world", sep=",")  # result: hello,world
print(3, 5, sep="*")  # result: 3*5

The print Function's end Keyword

By default, when we use the print statement, it ends with a newline, making the next text appear 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

Packing and Unpacking

This part isn't only common to the print function—arguments and keyword arguments can be packed into other functions too, but I thought I shouldn't leave this out.

Instead of looping through a list to print each element, we can unpack the list directly in print:

days = ["Mon", "Tue", "Wed", "Thur", "Fri", "Sat", "Sun"]
print(*days, sep=", ")

Practical Example:

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")

Suppressing the print Function

The print function is a good debugging tool, but sometimes, we may want to remove all print statements. Instead of manually deleting them, we can disable the print function by overwriting 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")

/

Rubbie kelvin © 2025 Made with love & vue · Why does your portfolio look plain?

  • 00 ./~home
  • 01 ./projects
  • 02 ./blog
  • 03 ./contacts