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 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
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(“”).
print("Hello World!")
Hello World using single quote(”).
print('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.
id = 5
print(id)
Printing multiple variables separated by comma’s.
id = 5
name = "Sam"
print(id, name)
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.
print("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.
print("Hello" + "World!")
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.
day = "07"
month = "02"
year = "2021"
print(month,day,year)
With sep argument.
day = "07"
month = "02"
year = "2021"
print(month,day,year, sep = "-")
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.
print("Hello")
print("World!")
With end as tab.
print("Hello", end="\t")
print("World!")
space as end.
print("Hello", end=" ")
print("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.
print("""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.
#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:
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
Expected Output
Note: All this has to be done in the single line.