Python Tutorial



Python Tutorial

Print In Python


Python is extensively used for software development, system scripting, and server-side web development. It can generate server-side web applications, database system connections, and workflows, perform complex mathematical examples for handling Big Data, and fast prototyping.

Among the key features of Python is its capacity to output data to the normal output or console. The print in Python function is utilized to show variables or text on the screen. It is an embedded function that can be applied to every Python program without needing extra installation.

An Overview Of Print In Python

Python's print () function is employed to print the preferred message on a device's screen. The Print always follows a string format. If the message of print is in different objects, it is initially changed into a string before getting printed. You can key in one or several objects of any kind. Prior to getting printed, the objects become altered into a string. The print in Python function is utilized to showcase variables, messages, and calculation results.

A better reference to the Python print() function operation lies in the Python print documentation. The Python print format signifies the manner in which you show output utilizing the print() function. It comprises formatting the data you wish to print to a file or the console in a readable and structured manner.

Python Print() Function Syntax

In contrast to Python 2, which did not need to apply parentheses, Python 3 needs parentheses to be applied, or it will display a syntax error. The print() function, which acknowledges 0 or more expressions divided by commas, is the easiest way to produce output. This function alters the expressions you approve into a string prior to writing to the screen.

The whole Python print() syntax is as follows:

Print (*objects, sep = ‘’, end = ‘\n’, file = sys.stdout, flush = False)

*object: one or greater objects to be printed.

Sep: Separator among objects. Default value = one space


a = ‘Introducing’
b = ‘Python’
print (a, b, sep = ‘,’)


“Introducing, Python”

End: The value gets printed after the entire described objects are printed.

Preset value = Newline


a = ‘Introduction’
b = ‘Python’
print (a, end = ‘ & ‘)
print (b)


“ Introduction & Python”

Print line or Python println means it will use \n at the culmination of the printed line.

File: Stream at the point of output printing. Default value = Normal output


Make a file named “” and insert the below code:

newfile = open( ‘ demo.txt ‘, ‘ w ‘)
print (‘ Welcome to the tutorial ‘)

Operate the program utilizing “python > output.txt.” It will generate a file “output.txt” and include the print text within it.

Flush: It is utilized to unbuffer and buffer the output. The preset value is “False,” whereby the output is buffered. If we fix the “flush = True,” then, the output becomes unbuffered and will have slow processing.

Demo = open(“demo.txt”, “a”)
demo.write(“One more line!”)

The list of python print() function arguments include;

  • Objects

  • Sep

  • End

  • File

  • Flush

How Print() Works In Python?

The Python Print() is utilized to get output or show anything on the screen and also to debug code. The function outputs a value or the supplied message to the console.

  • Print() function in Python is applied to print the particular object to the normal output device (screen) or else a text stream file.

  • The print() method in Python is employed to print a specified message to the screen or to show the value of an object on the terminal.

The Python print() function intakes any quantity of parameters and prints them out on a single line of text. Each item is changed to text form, divided by spaces, and there is one ‘\n’ at the closure (the “newline” character). When selected with zero parameters, print() simply prints the ‘\n’ and no other thing. In the interpreter, standard-out shows to the screen amid the ‘>>>’ prompts, affording a simple way to view what print() does.

>>> print (13, 26, -2)
13 26 -2
>>> print (‘hello,’ ‘there,’ -4)
hello there -4
>>> print (‘theme’) # 1 item, 1 \n
>>> print ()              # 0 items, 1 \n    

In source code of Python, the string text is written with quotes, such as ‘Welcome’, so we are familiar to viewing that form. Remember that printing a string simply prints out the string’s text data without any quotes:

>>> print (‘Welcome’)

Python Print() Function With Examples

The Python print() function is utilized to show any object or the text to the console or any normal output. When you employ a Python shell to examine statements, the normal output would be a shell console, while for real-time projects, we mainly select the logging as normal output, so it outputs each text from the print() to a log file.

By default, the print() function in Python shows to the normal console. Print() with no arguments shows the new line to the console, so when you print a string, it shows a string and also includes a new line within the console.

What will be the output of the print('*' * 10) in Python?

If the language is Python, then output:


Examples of Python print() Function

First, let’s view the default attitude of the Python() function that only takes the text.

By default, upon displaying text in Python using the print() function, every text published with a print function scrawls in a new line. A Python print example is the following.

# print() usage
print("Welcome to")
print("Python Tutorial")

This code produces the undermentioned output on the console.

Print Several Texts

The print() function can even be employed to print several statements, and you just need to pass the entire objects you wish to print as an argument to the function.

# Print multiple strings
Str1 = “Say hello to”
Str2 = “Python Tutorial”
print (str1, str2)

This code delivers the undermentioned output on the console.


# Say hello to Python Tutorial

Python String Literals

A string becomes literal and can be developed by scripting a text (a group of characters) enclosed by one(‘), double(“), or triple quotes. We can script multi-line strings or show them in the preferred way using triple quotes. Here nerdfornerds comprises a string literal that is allotted to a variable (s). Following is an example of a Python string literal.

# in single quote
s = ‘nerdfornerds’

# in double quotes
v = “nerdfornerds”

# multi-line String
m = ‘’’ nerd
print (s)
print (v)
print (m)



Python Character Literal

It also comprises a kind of Python string literal where one character is encompassed by one or double quotes.

# character literal in one quote
b = ‘r’

# character literal in double quotes
u = “p”




“End” Parameter In Print

The end parameter defines the string/character at which the printed value finishes. It is a line break by default.

Calling several print() functions includes a new line instantly. This is because, by default, every print () function call inserts a newline character ‘\n’ to the culmination of the row.

For example:

print (“Welcome”)
print (“Python”)



If you do not wish every print to make a new line, you can alter the value of the end parameter within the print() function call.

For example, let’s apply a blank space in place of a new line:

print (“Welcome,” end=” “)
print (“Python”)


Welcome Python

Tweaking the end parameter within the print() function can be helpful if you desire to alter the way successive prints display in the console.

“Flush” Parameter In Python

The flush parameter of print in Python enables you to select buffered or unbuffered output. This parameter has a default value of False, implying the output will be buffered. If you set this as True, the output is unbuffered, and this procedure is generally slower than the former.

“Sep” Parameter In Python

The separator parameter sep works as a separator when the print() function is evoked with several values. This is set as an empty string, ‘ ‘, by default.

If you desire to alter it, you must specify the separator by furnishing the arguments as a keyword argument within the print() function call. Specifically, you need to insert sep=’something’ to the print() function call.

For example, let’s separate every element by 2 new lines:

Print (4, 5, 6, sep=’\n\n’)



The separator is helpful if you wish to customize how several values are printed into the console.

File Argument In Python Print()

The file parameter defines where the print() functions to transmit the output. The output is sent into the console by default.

The print() function shows the output in the console by default. However, this is only one stream where the output may be shown. You can, for instance, print the results into a different text file.

Writing To A File With Python’s Print() Function

The print() function in Python is among the most extensively used functions. Developers mainly use it to show variable values and strings each in the compiler or the interactive interpreter. It completely depends on what the Python program is flowing.

Nonetheless, a possibility exists to alter its functioning from writing to console to writing text to a file. The following are the different methods of Python print to file.

1. Redirecting The Normal Output Data To A File

There are different operations and ways that programmers can work using print(). Besides printing data to the console, it also aids in directing texts to a location termed the standard output stream (stdout). The other 2 streaming pipes are stderr and stdin.

As default, the standard output (stdout) pipe targets the interactive window that aids in program execution. The standard output can also be redirected to different locations, like text files. We can utilize the print() function here with different approaches or parameters.


import sys
print (‘Displaying this message on the output console.’)
orig_stdout = sys.stdout
with open(‘dataFile.txt’, ‘w’) as gr:
    # Changing the standard output
    sys.stdout = gr
    print ( ‘Writing the codename GR26 to the newly created file.’ )
    sys.stdout = orig_stdout


2. Redirecting By Standard Error Stream

When any error occurs in Python, it is scripted to the standard error stream (stderr). To publish the value of stderr within the file, we are required to redirect it. We can just redirect the sys.stdout to look to the sys.stderr, with the file parameter of the print() function. It is helpful while debugging little programs. Nevertheless, it is suggested to utilize a debugger with big programs.


import sys
print ( ‘Displaying this message on the output console.’ )
orig_stdout = sys.stdout
sys.stdout = sys.stderr
print ( ‘Writing the codename GR26 to the newly created file.’ )
with open( ‘dataFil.txt,’ ‘w’ ) as gr:
    sys.stderr = gr
    print ( sys.stderr , file = sys.stderr )
    sys.stdout = orig_stdout


3. Printing Particular Value To A File

Rather than writing any normal pipe value, programmers can even write particular values to a file applying the print (0) function. If the file is unavailable, this process builds a new file with that name and writes the string values approved to the print().


sampl = open( ‘ AnyFile.txt, ‘‘w’ )
print ( ‘Karlos, Dee, Sue, Bill, Steve, Elon’, file=sampl )



The Python print function is an inbuilt function that is employed to show variables or text on the screen. It is among the key features of  Python and is used in different applications, including machine learning, data science, and web development. The print function syntax is plain, using one or more objects as input and showcasing them on the screen. The print function does not give back any value but is utilized to output data to the standard output or the console.


  1. How do I print a formatted string utilizing the print () function?

By applying the string formatting syntax, you can print a formatted string employing the print () function. For instance, print (“The answer is {}.”. format (56) will output The answer is 56. Preferably, you can employ f-strings in Python 3.6 and newer, like this: print ( f”The answer is {56}.” )

  1. What is %s %d in Python?

“%s” and “%d” are the string formats in Python. “%d” is used for the numbers, and “%s” is employed for the strings.

Leave a Reply

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