Guiding Questions:

  • What is the difference between print() and using the end parameter?
  • What happens if you print something with nothing inside the parentheses?
  • What does a comma mean inside the parentheses?

The Anatomy of a Print Statement

In Python, the print statement looks like print(). Unlike Java, you don’t need to write System.out because Python is designed to be much simpler and more direct.

  • print: This is the actual function that tells the computer to send information to the screen.
  • The Parentheses (): These act as a container for whatever you want to show the world.

The Different Ways to Print

In Java, you have to choose between print() and println(). In Python, the behavior is built into one function:

  • Standard print(): After you print something, the cursor automatically moves to the next line. This is the default behavior, similar to Java’s println().
  • Staying on the same line: If you want the cursor to stay on the same line (like Java’s print()), you add end=”” inside the parentheses. This tells Python not to jump to the next line yet.
  • Formatted Strings: While Java uses printf(), Python uses “f-strings.” By putting an f before your quotation marks, you can easily format decimals or align your text.

3 Different Ways to Utilize This Method

  1. print(variable): If you have a variable assigned to any number, letter, or word, you can print it out just by putting that variable name inside the parentheses.
  2. print(“Word”): You can print a direct string by putting it in quotation marks. When it outputs, the quotation marks won’t be there—only the text inside them will show up.
  3. The Comma (,): You can use a comma to combine multiple things you want to print together.
    • Variable, variable: It will print both values side-by-side with a space automatically added between them.
    • “String”, “string”: This will put the second string after the first one, with a space in the middle.
    • Variable, “string”: This is very helpful because it lets you print a number and a word together without any errors. Python handles the different types for you!

Nothing

If you print nothing—print()—it will just move your cursor to the next line. It is a great way to add a “blank” line in your code output to make it easier to read.

Review

So Python print statements are much shorter and feel a lot more natural than the complex statements in Java. These print statements can handle variables, strings, and empty lines with very little code. Just remember that the comma is your best friend for combining different pieces of information! Be ready for the next Python blog lesson, and I will see you then. Bye!

 

javastrings.png

Click to Call Us