The python print function is one of the built-in function used to print 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 with multiple examples. The basic syntax behind this Python print function is
print(value,……., sep = ‘ ’, ends = ‘\n’, file = sys.stdout, flush = False)
Let me see the detailed information behind this Python print function arguments:
- Value: This print function prints this value to output or writes it to a file using file argument.
- sep: It’s an optional argument. If you specify any string then that string value inserted between values. By default, this value is space.
- end: It’s an optional argument of Python print function. 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 print 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 examples
In this example, we are using the Python print function to print Hello world as an output.
# Python Print Examples print("Hello World") print('Hello World!')
In Python print function, you can use either a double quotation or a single quotation or combination of both to retune the string as an output.
Here, we are printing two individual strings, “Learn” and ‘Python Programming’ separated by comma. As we said earlier, it uses space as the default separator so, it prints Learn Python Programming
print("Learn", 'Python Programming') print("Learn", 'Python', 'Programming')
Use the below statements to print an empty line because, by default, the print uses a newline as the argument value.
print() print(' ')
I think you might not have noticed the empty spaces. So, let me use the string in-between those statements. This example prints two empty lines, Hello world, an empty line, Learn Python Programming, a new line, and Hi.
print() print(' ') print("Hello World") print() print('Learn', 'Python Programming') print(' ') print('Hi')
Python print variables
When you are calling a variable or printing the variable value, you have to call its name without any quotations. Here, we declared a number and assigned 100. Next, we called that number without quotation and with a quotation.
The first print statement is calling the variable number value. The second print statement is printing the word number. So, please be careful while calling the variable values or list values, etc.
number = 100 print(number) print('number') name = 'Tutorial Gateway' print(name) print('name') print('Number = ', number) print('Name = ', name)
Python print List, Tuple, Set and Dictionary
Using this print function to print the items in Lists, Tuples, Sets, and Dictionary. As you can see from the below, we created two lists, two tuples, three sets, and 1 dictionary. Next, we used the Python print function to print them.
If you want to access each item individually or to perform operations on each item, then use For Loop along with range Function.
numbers_list = [10, 20, 30, 40, 50] fruits_list = ['apple', 'cherry', 'mango', 'kiwi'] print(numbers_list) print(fruits_list) numbers_tuple = (15, 25, 35, 45, 55) fruits_tuple = ('apple', 'cherry', 'kiwi', 'mango') print(numbers_tuple) print(fruits_tuple) numbers_Set = {1, 2, 3, 4, 5} fruits_Set = {'apple', 'kiwi', 'banana', 'orange'} mixed_Set = {'banana', 1, 2, (1, 2, 3)} print(numbers_Set) print(fruits_Set) print(mixed_Set) myDict = {'name': 'Kevin', 'age': 25, 'job': 'Developer'} print(myDict)
Python print sep example
By default, the range function uses the empty space as the separator. However, you can override this with any of your own string. The third statement in this Python print sep example uses * as the separator between two strings Hello and World. The next statement uses — as the separator.
print("A", "B", "C", sep = "") print("A", "B", "C", sep = " ") print("Hello", "World", sep = "*") print('Learn', 'Python Programming', sep = "---") print('Learn', 'Python', 'Programming', sep = " ") print('Learn', 'Python', 'Programming', sep = " # ") print("Hello", "World", sep = " AAA ") print("Hello", "World", sep = ',,,...,,,')
Python print end example
How to use print function without a new line?. By default, the range function uses the new line as the end argument value. However, you can override it. The first two statements in this python print end example prints the strings in two different lines.
In the fourth statement, we used the end argument with space as the value and the fifth statement with . as the argument value. It means, after printing the Welcome to, space appended. Next, the fifth print statement prints the Tutorial Gateway after that space, and the statement ends with.
print('Welcome to') print('Tutorial Gateway') print('-----') print('Welcome to', end = ' ') print('Tutorial Gateway', end = '.')
Python Print end and sep arguments
In this Python print function arguments example, we are using both the sep argument and the end argument. From the below, print(10, 20, 30, sep = ‘ @ ‘, end = ‘ **** ‘) prints 10 20 30 separated by @ symbol and ends with ****. Similarly, print(‘A’, ‘B’, ‘C’, sep = ‘ , ‘, end = ‘ ##### ‘) statements prints A B C separated by comma and ends with #####
print(10, 20, 30) print('A', 'B', 'C') print(10, 20, 30, sep = ' , ') print('A', 'B', 'C', sep = ', ') print(10, 20, 30, sep = ' @ ') print('A', 'B', 'C', sep = ' -- ') print('-------------') print(10, 20, 30, sep = ' @ ', end = ' **** ') print('A', 'B', 'C', sep = '--', end = ' . ') print() print('A', 'B', 'C', sep = ' , ', end = ' ##### ') print(10, 20, 30, sep = ' @ ', end = ' !!! ')
It is another example of the Python print function arguments. Here also, we used the print function along with the separator argument and the end argument.
For example, print(‘Number = ‘, num, sep = ‘0000’, end = ‘?\n\n\n’) prints number separated by 0000 and ends with ? and three new lines.
num = 1234 print('Number = ', num, sep = '****', end = '!') print() print('Number = ', num, sep = '0000', end = '.') print() print('Number = ', num, sep = '0000', end = '?\n\n\n') print('Number = ', num, sep = '0000', end = '@\n')
Python For Loop Print Example
Use this print function inside the For Loop along with Range Function. By this, you can print items within a given range — the first statement print numbers from 1 to 9.
for i in range(1, 10): print(i) print("--------------") for i in range(1, 10): print(i, end = " ") print("\n--------------") for i in range(1, 10): print(i, end = " , ")
Python print String Format
We are using the print function along with conversion types. Within the first print statement, we used two %s in between a string follows by a tuple with variables. It means print function replace those two %s items with those tuple values.
Here, Python print function uses the same order that we specified. I mean, first, %s is replaced by a person variable, and the second %s replaced by name variable value. Here, print(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('Copyright %s at %d' %(name, year)) print(person, ' is working at ', name) print(person + ' is working at ' + name) print('Copyright ', name, ' at ', year)
Python print format Example
It is an example of a Python print format function. In this example, we are using the format function inside the print function. It allows us to format values. I suggest you refer to the Python 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))
Special Characters in Python Print Function
The Python programming language 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 print statement. Next, we used \ to escape ‘(single quote) in print (‘I Can\’t Do that’).
s = 'Hi there, "How are You?"' s1 = 'Hi there, \"How are You?\"' print(s) print(s1) print('I Can\'t Do that') print('I Don\'t Know you')
The first print statement returns three new lines. And the last statement prints 5 new lines.
print(3 * "\n") print("--------") print('\n\n\n\n\n')
This print function example is the same as the above — however, this time, we used three empty lines in between the two strings Hi and Hello.
print(3 * "\n") print('-----------') print('Hi') print(3 * "\n") print('Hello')
In this example, we are using the New Line, Horizontal Tab, Vertical Tab, etc., in between a string to show you how they alter the output. For example, the first statement with \n prints Hi there, in the first line and How are You? in the next line.
s = 'Hi there, \nHow are You?' s1 = 'Hi there,\tHow are You?' s2 = 'Hi there,\vHow are You?' s3 = 'Hi there,\fHow are You?' s4 = 'Hi there,\rHow are You?' print(s) print(s1) print(s2) print(s3) print(s4)
Conversion Types in Python Print function
The list of conversion types that are available in the Python print function.
- %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. For this, we declared a few variables with a numeric value, string, decimal value, and a character.
value = 100000 text = 'Tutorial Gateway' science = 1.20023456341 cr = 'u' print('%d' %value) print('%i' %value) print('%u' %value) print('%o' %value) print('%x' %value) print('%X' %value) print('%f' %science) print('%e' %science) print('%E' %science) print('%g' %science) print('%G' %science) print('%c' %cr) print('%s is a string representation' %value) print('%s' %text)
Python print file example
Here, we are opening a file pythonSample.txt (if it exists). Otherwise, it creates that text file in the default directory. Next, the print function prints the statement inside that text file.
file_name = open('pythonSample.txt', 'w') print('Welcome to Tutorial Gateway', file = file_name) file_name.close()