How to Use the print( ) Function in Python
Welcome to Python Programming!
Learn how to use the print() function, one of the most important tools in Python. The print() function helps you display information like text, numbers, and even calculations on the screen.
1. What is the print() Function?
The print() function is a command that tells Python to display something on the screen.
For example, if you want to say "Hello, World!" in Python, you can use this code:
print("Hello, World!")
When you run this code, Python will show this:
Hello, World!
2. How to Use the print() Function
a. Displaying Text
Text must be inside quotation marks ("" or '').
Example:
print("I love music!")
Output:
I love music!
b. Displaying Numbers
Numbers don’t need quotation marks.
Example:
print(42)
Output:
42
c. Combining Text and Numbers
You can combine text and numbers in one print() statement using a comma.
Example:
print("I am", 12, "years old.")
Output:
I am 12 years old.
d. Performing Calculations
Python can do math inside the print() function.
Example:
print(5 + 3)
print("The result of 7 * 4 is:", 7 * 4)
Output:
8
The result of 7 * 4 is: 28
e. Using Special Characters
Use special characters to format text, like \n for a new line.
Example:
print("Hello\nWorld!")
Output:
Hello
World!
3. Practice Exercises
Now it’s your turn to try!
Exercise 1: Simple Printing
Write a program that prints the following:
Your name
Your favorite color
The number 100
Example Output:
My name is Alex.
My favorite color is blue.
100
Exercise 2: Combining Text and Numbers
Write a program that displays:
Your age in a sentence (e.g., "I am 13 years old.")
Your birth year calculated using subtraction (e.g., "I was born in 2010.")
Exercise 3: Fun Math
Write a program that:
Prints the result of adding 15 and 25.
Prints the result of dividing 100 by 4.
Prints the result of multiplying 8 by 7 in a sentence like this:
8 multiplied by 7 is 56.
4. Bonus Challenge
Create a mini introduction program!
Write a Python script that introduces yourself with:
Your name
Your favorite hobby
Your age
A fun fact about yourself
Example Output:
Hello! My name is Mia.
I love playing soccer.
I am 12 years old.
Fun fact: I can solve a Rubik's cube in 2 minutes!
5. What’s Next?
Once you’re comfortable using the print() function, you can start learning about variables to store and use information in your programs.
Great job getting started with Python! 🎉