Blog

Python Print

  • (4.0)
  •   |   515 Ratings

print is used to display output into standard output device (Monitor console).
In Python 2.x.x print is a statement and in python 3.x.x print is a function.
Python 2.x.x: 

Learn how to use Python, from beginner basics to advanced techniques, with online video tutorials taught by industry experts. Enroll for Free Python Training Demo!

The arguments are:

1. objects - object to the printed. * indicates that there may be more than one object
2. sep - objects are separated by sep. Default value: ' '
3. end - end is printed at last
4. file - must be an object with write(string) method. If omitted it, sys.stdout will be used which prints            objects on the screen.
5. flush - If True, the stream is forcibly flushed. Default value: False

print() does not return any value.

Formatted String

To create a formatted output, we use a special operator %. It takes two operands. First one is the formatted string and second one is the value. The value can be a single value a tuple or a dictionary. 
The conversion specifier tells python how to convert the value. 
Example: 
print ("hello %s" % "User")
output: hello User
the conversion specifier here is %s which means string.
print("Ten=%s" % (5*2))
output: Ten=10
print("%s=%s" % ("Ten", 10))
output: Ten=10

Frequently Asked Python Interview Questions & Answers

Printing Special Characters

print (u"u00a9")
output: ©
to print special characters, we need to pass the Unicode character for that special character. 
The special character prints the character that cannot be used for print directly

character

Decimal

Description

 

 

statement continues on next line

 

92

backslash

39

Single quote

34

Double quote

a

7

Bell

b

8

Backspace

f

 

Formfeed

n

10

newline

r

13

carriage return

t

9

tabulation

v

11

vertical tabulation

? ?00

 

null value

ooo

 

octal value o in (0..7)

xhh

 

hexadecimal value (0..9, a..f; A..F)

uxxxx

 

Unicode character value

Conversion Specifier

character

Description

%c

Converts to a single character

%d,%i

Converts to a signed decimal integer or long integer

%u

Converts to an unsigned decimal integer

%e,%E

Converts to a floating point in exponential notation

%f

Converts to a floating point in fixed-decimal notation

%g

Converts to the value shorter of %f and %e

%G

Converts to the value shorter of %f and %E

%o

Converts to an unsigned integer in octal

%r

string generated with repr()

%s

Converts to a string using the str() function

%x,%X

Converts to an unsigned integer in hexadecimal

Setting the width

To set the width we set an integer, if the converted value is shorted than the width then spaces are added.
 print("(%10s)" %  "example")
output: (    example)
print("(%-10s)" %  "example")
output: (example   )
We can use * to use dynamic formatting 
print  ('%*s  %*s' % (10, "hello", 10, "world"))
 
output:      hello       world

Explore Python Sample Resumes! Download & Edit, Get Noticed by Top Employers!Download Now!

 


Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules