DEV Community

Cover image for Mastering the print() Function in Python 🔍🐍
Shreyansh Kumar
Shreyansh Kumar

Posted on

Mastering the print() Function in Python 🔍🐍

The print() function is often the very first line of Python code beginners write:
print("Hello, world!")
But there's more power packed into print() than most realize.

What is print()?
In Python, print() is a built-in function that outputs text or other data to the console. It's commonly used for debugging, displaying results, and providing user feedback.

🛠️ Basic Usage

print("This is Python.")
This is Python.
Enter fullscreen mode Exit fullscreen mode

You can also print numbers and variables:

name = "Alice"
print("Hello", name)
Hello Alice
Enter fullscreen mode Exit fullscreen mode

📌 Separator (sep) and End (end) Parameters
You can customize the way multiple items are separated and how the line ends.

print("Python", "is", "awesome", sep=" - ")
Python - is - awesome
Enter fullscreen mode Exit fullscreen mode
print("This is", end=" >> ")
print("a chained print.")
This is >> a chained print
Enter fullscreen mode Exit fullscreen mode

🔄 Print with Formatting

You can format your output using f-strings:

age = 25
print(f"I am {age} years old.")
I am 25 years old.
Enter fullscreen mode Exit fullscreen mode

📝 Final Thoughts
print() is simple yet incredibly versatile. Whether you're learning Python or building large-scale apps, mastering print() will make your coding life much smoother.

👉 If you liked this post, leave a like and follow me for more Python tips and tutorials!

Top comments (0)