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:
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:
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
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:
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:
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:
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
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:
In our example, we have made use of the end
keyword argument, and set it to a string containing one space.
sep
The
(like separator) parameter allows you to specify a separator between the values you're printing. By default, it is a space character.sep
Example:
print("My", "name", "is", "Monty", "Python", sep=",")
Expected output:
Now try to use the print function to get the result below
* * * * * * * *** *** * * * * *****
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:
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:
Here, the expression {x + y}
is evaluated and its result is included in the printed string.
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.