Print function | BUS 104 – Python Exam

The print() function explained

Imagine yourself jotting a to-do list onto a Post-it Note.

Your writing content directly onto a Post-it.

That's exactly what the print() function does, except instead of writing content onto a Post-it, it writes content in the output.

print() enables you to write content directly in the output.

Coding a print() function

Let's write a print() function of the to-do list above!

Make sure to open a Trinket file to follow-along! (How to create a Trinket file?)

First, let's check out this template for print() functions.

print(content)

So, to write the first to-do item onto our webpage, we'll type the following into the content.

print("- Take out the trash")

But, what about the rest of our to-do items?

To include these, all we need to do is write additional print() functions.

print("- Take out the trash")
print("- Do the dishes")
print("- Order wig for date party")

Type the code above into your Trinket file. It should look like this now:

Run your file by pressing the "Run" button above your code. You should get the following output on the right of your screen:

How do I include empty lines?

What if we wanted our to-do list to look like this instead?

To include these empty lines in-between each to-do list item, all we need to do is include empty print() functions!

print("- Take out the trash")
print
print("- Do the dishes")
print
print("- Order wig for date party")

Just writing "print" will place an empty line in your output.

Manually type the above highlighted code into your Trinket file. It should look like this now:

Now run your file. You'll see each to-do item is separated by one line like the image below!

Printing integers and floats

We've currently been printing strings. We can print integers and floats as well!

Here's an example of printing an integer:

print(123)

Here's an example of printing a float:

print(150.52)

If you want more information on strings, integers, and floats, I've made Data Types | Python in 30 Minutes for you!

Practice problem

Create a new Trinket file and code a print() function that writes "Hello there!" on the first line, then "What's going on?" below it. Separate these sentences with an empty line.

Your final output should look like so:

Desired output of the practice problem, with "Hello there!" and "What's going on?" written below it

print("Hello there!")
print
print("What's going on?")

Next up, we'll learn how to take in user-inputted data with the input function. Click below to move on!

Python Cram Kit

Want to unlock (PREVIEW ONLY) content? Get your Python Cram Kit now!

ApplyPRACTICE PYTHON EXAM (PREVIEW ONLY)
ToolsPython Nerd Notes (PREVIEW ONLY)
ConceptPrint function
ConceptInput function
ConceptConcatenation
ConceptConditionals (if, elif, else)
ConceptWhile loops (PREVIEW ONLY)
ConceptFor loops (PREVIEW ONLY)
ConceptData Types (PREVIEW ONLY)
ConceptNested Conditionals (PREVIEW ONLY)
ConceptFlag Variables (PREVIEW ONLY)

Leave a Comment