16 Jun
16Jun

Introduction

In Python, the print() function is used to display text or other objects to the console or standard output (usually your terminal or command prompt). It takes one or more arguments and displays their values as text and takes two keyword arguments (built-in)

The print() function is a built-in function in Python, so you don't need to import any specific module to use it. You can directly invoke it in your Python code.

Here's an example, how to use the print() function:

print("Hello world")

Expected output: 

Hello world


Arguments in Print()

Inside the parentheses, you provide one or more Strings, numbers, characters, logical values or objects  that you want to display as text separated by a comma.

Example:

message1 = "Hello, World!"
message2 = 5
print(message1,message2)

# or u can use this
print("Hello, World!","5")

Expected output:

Hello, World! 5


As we can see in the example above the print() function contains two arguments  "Hello, world!" and "5" separated by a comma  and outputs them in one line and separated by a space


Multiple line 

 If you invokes the print() function twice like this:

print("Hello, World!")
print(5)

 you can see two separate lines in the console 

Expected output:

Hello, World!

5

the empty print() invocation is not as empty as you may have expected - it does output an empty line, or (this interpretation is also correct) its output is just a newline.

print("Hello, World!")
print()
print(5)

Expected output:

Hello, World!


5

There is another way to add a new line without by using \n. The backslash (\) has a very special meaning when used inside strings - this is called the escape character.
The letter n placed after the backslash comes from the word newline.
Both the backslash and the n form a special symbol named a newline character, which urges the console to start a new output line.

Example:

print("Python a versatile and popular programming language,\nsimplifies complex tasks, making it ideal\nfor diverse applications.")

Expected output:

Python a versatile and popular programming language,
simplifies complex tasks, making it ideal
for diverse applications.

As you can see, two newlines appear in the nursery rhyme, in the places where the \n have been used.

Attention:  If you want to put just one backslash inside a string that will cause an error

print("\")

Expected output:

  File "main.py", line 1
    print("\")
             ^
 SyntaxError: EOL while scanning string literal


Optional Parameters

The print() function has optional parameters that allow you to customize the output:

  • sep: Specifies the separator between the values. The default is a space (' ').
  • end: Specifies what should be appended at the end of the printed values. The default is a newline character ('\n').
  • file: Allows you to specify where the output is directed (by default, it's the console).
  • flush: Determines whether the output is flushed immediately (True or False). The default is False.

end

The end parameter specifies what character(s) should be printed at the end of the output. By default, it's a newline character ('\n'), which means that a new line is started after printing. You can change it to another character or an empty string if you don't want a new line.

Example:

print("My name is", "Python.", end=" ")
print("Monty Python.")

Expected output:

My name is Python. Monty Python.

In our example, we have made use of the end keyword argument, and set it to a string containing one space.

sep

The sep (like separator) parameter allows you to specify a separator between the values you're printing. By default, it is a space character.

Example:

print("My", "name", "is", "Monty", "Python", sep=",")

Expected output:

My,name,is,Python,Monty,Python.


Now try to use the print function to get the result below

    *
   * *
  *   *
 *     *
***   ***
  *   *
  *   *
  *****


f"" Syntax

The f"" syntax, which stands for "formatted string literals," is a feature introduced in Python 3.6 that allows you to embed expressions and variables within a string using curly braces {}. These expressions are evaluated and their values are inserted into the string when it's displayed.

Example 1:

name = "Georges"
age = 41
print(f"My name is {name} and I am {age} years old.")

Expected output:

My name is Georges and I am 41 years old.

In this example, the values of the name and age variables are inserted into the string when it's printed.

Example 2:

x = 6
y = 15
print(f"The sum of {x} and {y} is {x + y}.")

Expected output:

The sum of 6 and 15 is 21.

Here, the expression {x + y} is evaluated and its result is included in the printed string.

Conclusion

The print function in Python is a fundamental tool for displaying information in the console. It allows you to output text, variables, and expressions to the screen. When combined with f-strings (formatted string literals), it provides a concise and readable way to create formatted strings, making it easier to incorporate dynamic content and control the formatting of variables.
This feature is invaluable for debugging, data presentation, and generating informative output in Python programs.

Comments
* The email will not be published on the website.