Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Python Print Function

Last Updated on February 27, 2023 by Prepbytes

Python is one of the most popular programming languages used in various industries, including data science, machine learning, web development, and more. One of the fundamental features of Python is its ability to output information to the console or standard output. The python print function is used to display text or variables on the screen. It is a built-in function in Python, which means it can be used in any Python program without requiring any additional installation.

What is Print() Function in Python?

The python print function is a built-in function that allows you to display text or variables on the screen. It is used to output information to the console or standard output. You can use the print function to display messages, variables, and even the results of calculations.

Syntax of Print Function in Python:

The syntax python print function is straightforward. Here is the basic syntax:

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

The python print function takes one or more objects as input and displays them on the screen. The objects can be strings, numbers, variables, or even expressions. The objects are separated by a separator, which is a comma by default. You can change the separator by setting the sep parameter. The end parameter is used to specify what character should be used to terminate the output. By default, the end character is a newline. You can change the end character by setting the end parameter. The file parameter is used to specify the file where the output will be written. By default, the output is written to the console. Finally, the flush parameter is used to specify whether the output should be flushed immediately. By default, it is set to False.

Examples of Print Function in Python

Let’s understand python print function with the help of following examples.

Example 1: Using Separator and End Parameter

In this example we use print() with separator and End parameter in python

print("Hello ", "Prepbytes", sep='***', end='\n\n')

print("I'm below two lines")

Output :

Hello ***Prepbytes

I'm below two lines

Explanation: In the above program, we’ve passed the sep and end parameters.
A separator creates partitions between various objects/items within a print statement. The default value for this attribute is a whitespace character. The separating value, in this case, is ***.
The end parameter specifies the value that should appear at the end of all provided values. It is set to ‘n’ by default, which is a newline character. We used two newline characters in the preceding example. As a result, the following print() statement prints after two lines.

Example 2: Using File Parameter

In this example we use print() with File Parameters

requiredFile = open('python.txt', 'w')
print('I m writing to the file', file = requiredFile)
requiredFile.close()

Explanation: The file parameter has passed a requiredFile file object. The string object ‘I’m writing to the file’ is written to the file python.txt.
Finally, the close() method was used to close the file.

Example 3: Using End Parameter

In this example we use print() using end Parameters

string1='Hello'
string2='World'
print(string1,end=' & ')
print(string2)

Output:

Hello & World

Explanation: The print() function in the preceding example uses the end parameter as ‘&’.The value of the end parameter is visible because when the string1 ends, the next print function prints the string2 after the &.

Return Value of Print Function in Python:

The python print function does not return any value. It is used to output information to the console or standard output. However, you can use the return statement to return a value from a function. In that case, the value will be displayed on the screen using the print function.

Parameters of Print Function in Python

Here are the parameters that can be used with the python print function

  • value1, value2, …: One or more values or expressions that you want to print. They will be separated by the sep parameter if specified, and followed by the end parameter if specified.
  • sep: The separator to use between the values printed. It defaults to a space character (‘ ‘).
  • end: The character to print at the end of the line. It defaults to a newline character (‘\n’).
  • file: The file object to write the output to. It defaults to the console (sys.stdout).
  • flush: Whether to flush the output buffer after printing. It defaults to False.

Summary:
The print function in Python is a built-in function that is used to display text or variables on the screen. It is one of the fundamental features of Python and is used in various applications, including data science, machine learning, web development, and more. The syntax of the print function is straightforward, and it takes one or more objects as input and displays them on the screen. The print function does not return any value but is used to output information to the console or standard output

Frequently Asked Questions(FAQ):

Sure, here are some additional frequently asked questions about the python print function :

Q1: How do I print a blank line using the print() function?
Ans: You can print a blank line using the print() function by specifying an empty string as the value to print, like this: print(" ”).

Q2: How do I print a formatted string using the print() function?
Ans: You can print a formatted string using the print() function by using the string formatting syntax. For example, print("The answer is {}.".format(42)) will output The answer is 42.. Alternatively, you can use f-strings in Python 3.6 and later, like this: print(f"The answer is {42}.").

Q3: How do I redirect the output of the print() function to a file?
Ans: You can redirect the output of the print() function to a file by specifying a file object as the value of the file parameter. For example, with open("output.txt", "w") as f: print("Hello, world!", file=f) will write the text "Hello, world!" to a file named "output.txt" in write mode.

Q4: How do I print a string without a newline character using the print() function?
Ans: By default, the print() function adds a newline character (\n) at the end of the string that it prints. To prevent this, you can specify an empty string as the value of the end parameter, like this: print("Hello, ", end=""). This will print "Hello, " without a newline character.

Q5: How do I print a variable name and its value using the print() function?
Ans: You can print a variable name and its value using the print() function by using f-strings and the vars() function to access the variable’s value by name.

Q6: Can you change the separator between the values printed by the print() function?
Ans: Yes, you can change the separator between the values printed by the print() function by specifying the sep parameter.

Q7: Can you print text to a file instead of the console using the print() function?
Ans: Yes, you can print text to a file instead of the console using the print() function by specifying the file parameter.

Leave a Reply

Your email address will not be published. Required fields are marked *