Python print Function

Python print is one of the built-in functions used to display the given value as an output. If you want something to be written o the screen or output device, then you have to use this Python print function.

In this section, we explain to you how to use this Python print function of the standard library with multiple examples. The basic syntax behind this print function is

print(value,……., sep = ‘ ’, ends = ‘\n’, file = sys.stdout, flush = False)

Let me see the detailed information behind these arguments:

  • Value: This Python print function displays this value to output or writes it to a file using a file argument.
  • sep: It’s an optional argument. If you specify any string, then that string value is inserted between values. By default, this value is space.
  • end: It’s an optional argument. If you have given any string, then that string is appended after the last value. The default value is a newline (\n).
  • file: This is an optional argument. You can use this to display the output in a text file or something like that. By default, it uses sys.stdout (standard output)
  • Flush: Change it to true if you want to flush the stream forcibly.

Python print function examples

In the Python print function, you can use either a double quotation, a single quotation, or a combination of both to return the string literals as an output.

In this example, for the first statement, we are using it to display Hello World as an output. Remember, the print statement is the standard output device that prints blank lines by default.

Within the second line, we are displaying three individual strings separated by a comma. As we said earlier, it uses space as the default separator for string concatenation.

print("Hello World")

print("Learn",  'Free', 'Programming')
Hello World
Learn Free Programming

Use the Python print function without any argument to return an empty line because, by default, it uses a new line as the argument value.

I think you may not notice the empty spaces in the previous example. So, I suggest you use this method in between any of the statements.

How to use Variables inside a Python print function?

When you call a variable or display the variable value, you have to call its name without any quotations in the print function. Here, we declared a number and assigned 100. Next, we called that number without a quotation and with a quotation.

The first Python print function statement is calling the variable number value. The second one is stamping the word number. So, please be careful while calling the variable values or list values, etc.

Python print function 5

How to Print List, Tuple, Set, and Dictionary in Python?

You can use this Python print function to display the items in Lists, Tuples, Sets, and Dictionary. As you can see below, we created a list, two tuples, a set, and a dictionary. Next, we used it to display them.

If you want to access each item individually or perform operations on each item, then use For Loop along with the range Function.

numbers_list = [10, 20, 30, 40, 50]
fruits_tuple = ('apple', 'cherry', 'kiwi', 'mango')
mixed_Set = {'banana', 1, 2, (1, 2, 3)}
myDict = {'name': 'Kevin', 'age': 25, 'job': 'Developer'}

The output of the list, tuple, and set items

[10, 20, 30, 40, 50]
('apple', 'cherry', 'kiwi', 'mango')
{1, 2, (1, 2, 3), 'banana'}
{'name': 'Kevin', 'age': 25, 'job': 'Developer'}

Python print function with sep (separator) argument

By default, this function uses the empty space as the separator. However, you can override this with any of your own strings. The first statement in this print sep example uses * as the separator between two strings, Hello and World.

print("Hello",  "World", sep = "*")

Python print function with end parameter

How to use this print statement without a blank line? The range function default uses the new line as the end argument value. However, you can override it. In the above Python print function example, the two statements display the strings in two different lines.

Here, we used the end parameter with space as the value and the second statement with . as the argument value. It means, after stamping the Welcome to, space appended. Next, the fifth statement displays the Tutorial Gateway after that space, and the statement ends with.

print('Welcome to', end = ' ')
print('Tutorial Gateway', end = '.')
Welcome to Tutorial Gateway.

How to use end and sep arguments in the Python print function?

In this Python print statement arguments example, we use both the sep and end arguments. From the below, (10, 20, 30, sep = ‘ @ ‘, end = ‘ **** ‘) returns 10 20 30 separated by @ symbol and ends with **** within the same line. Similarly, (‘A’, ‘B’, ‘C’, sep = ‘ , ‘, end = ‘ ##### ‘) statements return A B C separated by a comma and end with ##### in the same line.

print(10, 20, 30, sep = ' @ ', end = ' **** ')
print('A', 'B', 'C', sep = '--')
print('A', 'B', 'C', sep = ' , ', end = '  #####  ')
10 @ 20 @ 30 **** A--B--C . 
A , B , C  #####

It is another example of a function with keyword arguments. Here also, we used this along with the separator argument and the end argument.

For example, (‘Number = ‘, num, sep = ‘0000’, end = ‘?\n\n\n’) displays numbers separated by 0000 and ends with ? and three new lines.

num = 1234
print('Number = ', num, sep = '0000', end = '?\n\n\n')
Number = 00001234?

How to use print statement inside a For Loop?

Use this function inside the For Loop along with Range Function. By this, you can display the items within a given range — the first statement returns numbers from 1 to 9. You can also use the end argument to display the numbers in one line. For instance, (i, end = ” , “) separates the numbers by command and displays them in one line.

for i in range(1, 10):

Python print String Format

We are using this function along with conversion types. Within the first statement, we used two %s in between a string, followed by a tuple with variables. It replaces those two %s items with those tuple values.

Here, it uses the same order that we specified. I mean, first, %s is replaced by a person variable, and second %s is replaced by the name variable value. Here, the (person +’ is working at ‘+ name) statement is to concat three items.

name = 'Tutorial Gateway'
person = 'suresh'
year = 2019
print('%s is working at %s' %(person, name))
print(person + ' is working at ' + name)

string formatting output

suresh is working at Tutorial Gateway
suresh is working at Tutorial Gateway

Python print function with format {} Example

In this example, we are using the format function inside this function. It allows us to format values. I suggest you refer to the format Function article.

name = 'Tutorial Gateway'
person = 'suresh'
year = 2019
print('{0} is working at {1}'.format(person, name))
print('Copyright {} at {}'.format(name, year))
suresh is working at Tutorial Gateway
Copyright Tutorial Gateway at 2019

How do handle Special Characters in the Python print function?

It allows you to use special characters by using escape characters. For example, within s1 (‘Hi there, \ “How are You?\”‘), we used \ character to escape double quotes within the statement. Next, we used \ to escape ‘(single quote) in (‘I Don\’t Know you’).

s1 = 'Hi there, \"How are You?\"'
print('I Don\'t Know you')
Hi there, "How are You?"
I Don't Know you

In this example, we use the New Line, Horizontal Tab, Vertical Tab, etc., in between a string to show how they alter the output. For example, the first statement with \n display Hi there, in the first line and How are You? in the next line.

With New Line, Horizontal Tab, and Vertical Tab output

Python print function 16

Similarly, if we use the \n for the empty lines as well. For instance, (3 * “\n”) returns three new lines. And the (“\n\n\n\n\n”) statement stamps 5 new lines.

Conversion Types in Python print function

The list of conversion types that are available in the Python print statement.

  • %c – Returns a Single character.
  • %d – Returns a Decimal Integer
  • %i – for Long integer
  • %u – Returns an unsigned decimal integer
  • %e, %E – Returns the floating-point value in exponential notation.
  • %f – Returns the floating-point value in fixed-point notation.
  • %g – Returns the shorter value of %f and %e
  • %G – Returns the shorter value of %f and %E
  • %c – Returns a Single character
  • %o – Returns an Octal value
  • %r – Generates string with repr()
  • %s – Converts the value to a string using str() function.
  • %x, %X – Returns the Hexadecimal integer.

Let me use all the available conversion types in one example. For this, we declared a few variables with a numeric value of 100000, string text = ‘Tutorial Gateway’, decimal value science = 1.20023456341, and a character cr = ‘u’.

Python print function 18

Python print file example

Here, we are opening a file Sample.txt (if it exists). Otherwise, it creates that text file in the default directory. Next, it displays the statement inside that text file object.

file_name = open('Sample.txt', 'w')

print('Welcome to Tutorial Gateway', file = file_name)


After running the above script, if you open the text file, it has the message Welcome to Tutorial Gateway.