• Ctrl+C+Python
  • Posts
  • ๐Ÿ Python Basics: Mastering Print Statements

๐Ÿ Python Basics: Mastering Print Statements

If you're just starting out with Python, one of the first things you'll encounter is the print() function. This simple yet powerful function allows you to display output in your program, making it an essential tool for debugging and interacting with users. In this post, we'll break down everything you need to know about print() statements in Python!

Take your learning to the next level with quick, aesthetic coding tutorials on my YouTube channel! ๐Ÿš€โœจ

๐ŸŽฅ Watch now: @CtrlCPython

Subscribe for bite-sized Python lessons with lofi vibes & clean code. โ˜•๐ŸŽถ 

1. The Basics: Printing Text

The print() function is used to display text (also known as strings) on the screen. Here's a simple example:

Output:

Hello, world!

The text inside the parentheses must be enclosed in either double quotes (โ€œโ€œ) or single quotes (โ€˜โ€˜).

2. Printing Numbers

You can also print numbers directly:

Output:

42
3.14

3. Combining Text and Variables

You can use print() to display the values of variables:

Output:

My name is Alice and I am 25 years old.

Notice how we separate values with commas (,). Python automatically adds spaces between them.

4. Formatting Output

If you want more control over formatting, you can use f-strings (introduced in Python 3.6) or .format():

Output:

My name is Alice and I am 25 years old.
My name is Alice and I am 25 years old.

5. Printing Multiple Lines

You can print multiple lines using either multiple print() statements or a multi-line string:

Output:

Line 1
Line 2
This is a
multi-line string.

6. Printing Without a Newline

By default, print() adds a new line after every call. If you want to print multiple statements on the same line, use end='':

Output:

Hello World!

๐Ÿ† Conclusion

The print() function is an essential tool for any Python programmer. Whether you're debugging code, displaying results, or creating interactive programs, mastering print() is a great first step in your Python journey.

What other Python basics do you want to learn about? Let me know in the comments! ๐Ÿš€๐Ÿ

Happy coding! ๐Ÿš€๐Ÿ

Digital Shade

Want more Python tips and tricks?

๐Ÿ”” Join my Beehiiv newsletter for exclusive cheat sheets, Python hacks, and expert tutorials!

Reply

or to participate.