Print() Function

07 Nov Print() Function


Introduction

A function is a piece of prewritten code that performs an operation. Python has numerous built-in functions. Perhaps the most fundamental built-in function is the print function.
print() function is used to display output in a Python program.

IN: print('Python programming is fun!')

OUT: Python programming is fun!


In Python you can enclose string literals in a set of single-quote marks (‘) or a set of double quote marks (“).

IN: print(“Python programming is fun!”)

OUT: Python programming is fun!


If you want a string literal to contain either a single-quote or an apostrophe as part of the string, you can enclose the string literal in double-quote marks and vice versa.

IN: print("Don't fear!")
OUT: Don’t fear!

IN: print(‘Best saying "health is wealth"!’)
OUT: Best saying "health is wealth"!


Using triple quotes (either “”” or ”’)

1. Triple quoted strings can contain both single quotes and double quotes as part of the string.

IN: print("""I'm reading "Hamlet" tonight.""")
OUT: I'm reading "Hamlet" tonight.


2. Triple quotes can also be used to surround multiline strings, something for which single and double quotes cannot be used.

IN: print("""One
Two
Three""")

OUT: One
Two
Three 


3. Triple quotes can also be used to write multiline comments. Hash (#) symbol is used to write single line comments.

"""
Created on Sat Oct 21 17:22:38 2017
@author: Reboot

This is a temporary script file.
"""


multiple print statements output, in a single line

The print function normally displays a line of output. that means three statements will produce three lines of output.
If you do not want the print function to start a new line of output when it finishes displaying its output, you can pass the special argument end=’ ‘ to the function, as shown
in the following code:

print('One', end=' ')
print('Two', end=' ')
print('Three')


Output:
One Two Three
#end character could be any possible character.


Specifying an Item Separator

When multiple arguments are passed to the print function, they are automatically separated by a space when they are displayed on the screen.
like this:

IN: print('One', 'Two', 'Three')
OUT: One Two Three 


if we do not want a space printed between the items, you can pass the argument sep=”
to the print function, as shown here:

IN: print('One', 'Two', 'Three', sep='')
OUT: OneTwoThree


Escape Characters

An escape character is a special character that is preceded with a backslash (\), appearing inside a string. These are treated as special commands that are embedded in the string.
For example, \n is the newline escape character. When the \n escape character is printed,it isn’t displayed on the screen. Instead, it causes output to advance to the next line.

IN: print('One\nTwo\nThree')

Out: 
One
Two
Three

Some of Python’s escape characters

Python Escape Characters list

Python Escape Characters list

No Comments

Post A Comment

Powered by themekiller.com anime4online.com animextoon.com apk4phone.com tengag.com moviekillers.com