Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

What is print in Python

Understanding the Print Function in Python

When you're starting out with programming, one of the first things you'll want to do is see the results of your code. That's where the print function comes in handy in Python. Think of print as a way to send a message from the world of code to the human world, allowing your program to communicate with you by displaying text on the screen.

What Does Print Do?

The print function in Python is like telling your program to shout out whatever you want. By using print, you can display almost anything: numbers, text, results of calculations, or even the contents of a variable (a storage container for data in your program).

Here's the simplest example:

print("Hello, world!")

When you run this code, Python will display the text Hello, world! on your screen. It's the most basic form of output, but it's incredibly powerful when learning how to program.

Variables in programming are much like labeled jars where you can store things for later use. When you create a variable in Python, you can use print to see what's inside your jar. For example:

greeting = "Hello, Python!"
print(greeting)

This code creates a variable named greeting that stores the text Hello, Python!. When you print the variable, it shows you the contents of the jar, not the jar itself.

Combining Text and Variables

You can also mix text and variables together in a print statement to create a more complex message:

name = "Alice"
age = 30
print("My name is", name, "and I am", age, "years old.")

In this case, Python will print the text and the variable values together in one line, with spaces between each piece of data.

Formatting Output

Sometimes you want your message to look a certain way. Python allows you to format your output so it's nice and tidy. One way to do this is by using f-strings (formatted string literals). They let you embed expressions inside string literals, using curly braces {}:

name = "Bob"
age = 25
print(f"My name is {name} and I am {age} years old.")

This code will output the same message as before, but it's easier to read and write, especially when you have many variables.

Printing Multiple Lines

What if you want to print multiple lines of text? You can use triple quotes """ or you can use the newline character \n:

print("""This is the first line.
This is the second line.""")

Or:

print("This is the first line.\nThis is the second line.")

Both of these examples will print the text on two separate lines.

Special Characters in Print

Sometimes you might want to include special characters in your print statements, like a tab or a new line. These are called escape sequences and start with a backslash \. For example:

  • \n for a new line
  • \t for a tab

Here's how you can use them:

print("Hello,\nPython!")
print("Name:\tAlice")

The first print will output "Hello" and "Python!" on two separate lines, and the second print will output "Name:" followed by a tab space and then "Alice".

Debugging with Print

As a beginner, you'll often use print to check if parts of your code are working correctly. This is called debugging. By strategically placing print statements, you can see what's happening at different points in your program:

total = 0
for number in range(1, 5):
    total += number
    print(f"After adding {number}, total is now {total}")

This loop adds numbers 1 through 4 to total, and the print statement shows the value of total after each addition.

Print's Parameters: end and sep

The print function has additional parameters that control how the output is formatted. The end parameter defines what to print at the end of the output. By default, it's a newline character, but you can change it:

print("Hello,", end=" ")
print("world!")

Here, instead of printing a new line after "Hello,", Python will print a space, resulting in "Hello, world!" on the same line.

The sep parameter defines the separator between multiple arguments. By default, it's a space:

print("Python", "Java", "C++", sep=", ")

This will print "Python, Java, C++", with a comma and a space separating the words.

When Not to Use Print

While print is useful, it's not always the best tool for every job. For example, if you want to save data to a file or display a message in a graphical user interface (GUI), you'll need different tools. print is mainly for simple text output to the console (the text-based interface you're using to interact with Python).

Conclusion: The Power of Simplicity

As you embark on your programming journey, the print function will often be your trusty companion, offering a window into the inner workings of your code. It's a simple yet powerful tool that helps bridge the gap between the abstract world of programming and the tangible feedback you need to learn and debug effectively.

Remember, programming is like learning a new language or an instrument; it takes practice and patience. Each time you use print, think of it as striking a chord or forming a sentence, bringing you one step closer to fluency in the language of technology. So go ahead, print away, and watch your code come to life, one line at a time.