Python print() Function


Python print() Function

In Python, the print() function is used if we want to print “anything” on the standard output device. Here “anything” can be a string, a variable, or any python object.

print() Function
Print Function

Print is a function in python followed by the parentheses() and, inside the parentheses, we specify what we want to print.

Syntax

print(object(s), end=end, sep=separator, file=file, flush=flush)

Parameters

Print Parameters

Printing strings

We can print a string either using the single quote(”) or double quotes(“”).

Example : print(“Hello World!”)

Output : Hello World!

Hello World in Python using double quotes(“”).

In [1]:
print("Hello World!")
Hello World!

Hello World using single quote(”).

In [2]:
print('Hello World!')
Hello World!

Printing variables

The print function is also used to print variables. For that, we have to pass the variable(s) in the parenthesis. In case, we are using the variables, make sure they are separated by commas.

In [1]:
id = 5
print(id)
5

Printing multiple variables separated by comma’s.

In [2]:
id = 5
name = "Sam"
print(id, name)
5 Sam

Print Concatenation

In the print() function, we can concatenate strings or objects using the comma(,) or plus(+) sign as a separator.

Using comma(,) for concatenation

When we use the comma(,) for concatenation of the strings or objects, a space is added in between.

In [1]:
print("Hello", "World!")
Hello World!

Using plus(+) for concatenation

When we use the plus(+) for the concatenation of the strings, then no space is added in between. Note: Plus(+) can be used with strings only. Also, this is not considered as the standard approach.

In [1]:
print("Hello" + "World!")
HelloWorld!

Python print sep argument

If we want multiple objects to be separated by a custom separator, then we use sep as an argument. By default, it is a space. Note: It will work only if we are using a comma(,) to concatenate the multiple objects, not with the plus(+).

Without sep argument.

In [1]:
day = "07"
month = "02"
year = "2021"
print(month,day,year)
02 07 2021

With sep argument.

In [2]:
day = "07"
month = "02"
year = "2021"
print(month,day,year, sep = "-")
02-07-2021

Python print end argument

By default, python uses a newline at the end of each print statement. We can use an end argument to override the default functionality. For example, we may need tabs(“\t”) at the end.

Without end argument.

In [1]:
print("Hello")
print("World!")
Hello
World!

With end as tab.

In [2]:
print("Hello", end="\t")
print("World!")
Hello	World!

space as end.

In [3]:
print("Hello", end=" ")
print("World!")
Hello World!

Printing pre-formatted text

To print the pre-formatted text, we can use single triple quotes(”’) or double triple quotes(“””). Anything inside the triple quotes will be printed as it is, along with the spaces, tabs, new lines, etc. This cannot be printed if we use single or double quotes.

In [1]:
print("""Hello,
    How are you?

Thanks,
Logical Python""")
Hello,
    How are you?

Thanks,
Logical Python

Printing to the File

The most common use of the print function is to print to the standard output. However, the print() function can also be used to print to the file.

In [1]:
#Open a file in write mode ('w')

with open('text_file.txt', 'w') as file:
    
    # using file parameter to print to file
    print('Hello, world!', file=file)

Once you execute this code, you will get output as:

Printing to file using the print function
Screenshot of file.

In this example, the open function is used to open a file named ‘text_file.txt’ in write mode (‘w’). The with statement ensures that the file is properly closed after writing. The print function is then used to write text to the file by using the file parameter with the opened file object.

After running this code, you will find a file named ‘text_file.txt’ in the same directory as your Python script, containing the printed text.

Just note that using the with statement is a good practice as it automatically handles the opening and closing of the file, ensuring that resources are managed efficiently.

Resources

Exercise QuestionHandwritten Notes

Expected Output

In [1]:
 
Where is Tim's Notebook
print('What to Print')
Steve said "You are right"

Note: All this has to be done in the single line.

References

  1. Print and Out
  2. Input and Output
  3. Guide to Python Print