CyberKeeda In Social Media

Python : Print() examples with parameters

Print

Nothing more is required to explain what does the print function does, it's name is itself self explanatory.
Print function is used to display /print messages into the screen.

Within python we can use ether pair of single inverted commas '  ' or double inverted "  "  within single brackets (  )  both gives the same result.

>>> print  ('Hello World')

Hello World

Print() with Parameters.

I'm adding some of the print function usage along with various parameters.

Print() with Sep

SEP commonly a short form of separator,  Separator between the arguments to print() function in Python is space by default (softspace feature) , which can be modified and can be made to any character, integer or string as per our choice.

Default behaviour of python with softspace.

>>> print('A','B','C')
A B C

#Trick to disable softspace using sep parameter
>>> print('A','B','C',sep='')
ABC

One more example.

>>> print('13','11','2018')
13 11 2018

#Trick to format a date
>>> print('13','11','2018', sep='-')
13-11-2018

Print() with End

End parameter along with print can be used to align data into a same single line along with required character,symbol or word.
Example:

>>> print('Welcome')
>>> print('to')
>>> print('cyberkeeda.com')

Output:
Welcome
 to
Cyberkeeda.com
Now, with help of end parameter, we can allign all these three different lines by one, along with extra characters too

>>> print('Welcome', end '  ')
>>> print('to', end '  ')
>>> print('cyberkeeda.com', end ' ! ')
Output:
Welcome to Cyberkeeda.com !

One more example ( SEP with END )
>>> print(10,20,30,sep=':', end='...')
>>> print(60,70,80,sep='-')

Output:
10:20:30...60-70-80

Print Formatted strings and Variables on the same line.

Before knowing anything, please note the below abbreviations.

%i==>  int
%d==> int
%f==>  float
%s==>  str

print("formatted String" %(variable list))

Examples.

>>> print("value is :%d" %(12))
value is  : 12

>>> print("My name is :%s and age is :%d" %('Cyberkeeda',23))
My name is : Cberkeeda and age is  : 23


No comments:

Post a Comment

Designed By Jackuna