Python Print Function

The Python print Function is a simple but powerful tool that lets you show messages or results on the screen. Whether it’s a greeting, a number, or the output of some calculation, print makes it easy to display information. It's super useful when you're starting out and need to check how things are working.

Here are some common ways to use it:

  • Displaying text: Show messages like "Hello, World!" or display prompts to the user.
  • Debugging: Print out values to see if your code is doing what you expect.
  • Showing results: Display the result of calculations or operations.
  • Printing variables: Check the values stored in your variables.
  • Combining information: Print different types of data, like text and numbers, in one line.


What You'll Learn

You will learn the basics of the print function in Python to display messages and numbers. We’ll focus on the fundamentals for now, so no need to worry about variables or data types.


Understanding the print Function

The basic syntax for the print function is:

python
print(object, sep='', end='', file=sys.stdout, flush=False)
  • object: The item (text, number, etc.) you want to print to the screen.
  • sep: The separator between multiple objects being printed (default is a space).
  • end: Specifies what comes after the printed object (default is a newline).
  • file: Defines where the output should go (default is the console).
  • flush: A Boolean value that specifies if the output stream should be flushed (default is False).

Don’t worry about the sep, end, and flush parameters for now! These are more detailed options that we will cover in later chapters. For now, just think of the print function as a simple tool that takes an input (like a message or a number) and displays it on the screen.

If you're curious and want to dive into sep, end, and flush right now, click here to learn more.


Example 1: Printing a simple message

To print a simple message, pass the message inside the print function with quotation marks to display it on the screen.

python
print("Welcome to Python Tutorial")

How It Works:

  • print: This is the Python function used to display something on the screen.
  • "Welcome to Python Tutorial": This is the message you want to display. It’s a string enclosed in quotation marks (either single or double quotes work).
  • When the code is run, Python takes the string "Welcome to Python Tutorial" and sends it to the screen using the print function. The result is that the message is shown in the output.

Output

Welcome to Python Tutorial

Example 2: Printing Numbers from 1 to 3

To print numbers, you simply pass the numbers to the print function without quotation marks, as Python recognizes them as numeric values. Here’s how you can print multiple numbers:

python
print(1)
print(2)
print(3)

How It Works:

  • print: This is the Python function used to display something on the screen.
  • 1, 2, 3: These are the numbers you want to display. Numbers don't need quotation marks around them because they are recognized as numeric values in Python.
  • When you run the code, Python sends each number to the screen one after the other, printing each on a new line by default.

Output

1
2
3

Example 3: Printing a Message and a Number

You can also pass multiple parameters to the print function. For example, you can print a message and a number together. The print function will print them in the same line, separated by a space by default.

python
print("You have", 3, "apples.")

How It Works:

  • print: This is the Python function used to display something on the screen.
  • "You have": This is a string (text) that will be printed on the screen. It is enclosed in quotation marks.
  • 3: This is a number, which Python will automatically print without quotation marks.
  • "apples.": This is another string, also enclosed in quotation marks, that will be printed after the number.
  • When you run the code, Python prints each parameter in the order given, separated by a space (this is the default behavior). In this case, it prints: "You have 3 apples."

Output

You have 3 apples.

Exercises

Try out the following exercises to practice using the print function.

1. Write a program that prints your name.
python
# Exercise 1: Print your name
# Replace 'your Name' with your name
print("Your Name")

2. Write a program that prints your country name.
python
# Exercise 2: Print your country name
# Replace 'your Country Name' with your country name
print("Your Country Name")

3. Write a program that prints your age.
python
# Exercise 3: Print your age
age = 20 # Replace age with your actual age (e.g., 21)
# The print function will display the value of age
print(age)

*Tip: After completing an exercise, feel free to experiment with the print function further to explore how it works!


Frequently Asked Questions

What does the print() function do in Python?

The print() function in Python displays the specified message or value on the screen. It's commonly used to show output, debug code, or communicate with users.


Can I print numbers and text together in Python?

Yes, you can print multiple values, such as text and numbers, in the same line using commas in the print() function.


Do I need quotation marks for everything I print?

Only text (also called strings) needs to be inside quotation marks. Numbers and variables can be printed directly without quotes.


Why do my printed values appear on new lines?

By default, Python’s print() function adds a newline character after each call. You can change this behavior using the end parameter.


Is print() useful for debugging?

Absolutely. print() is one of the simplest and most effective ways to check the values of variables and the flow of your program during debugging.



What's Next?

Next, you'll learn about variables, which are essential for storing and working with data in your programs. Understanding how to use variables will help you handle a wide range of tasks, from calculations to text manipulation.